CodeIgniter Forums
Email Helper only sends one of three emails successfully - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Email Helper only sends one of three emails successfully (/thread-22826.html)



Email Helper only sends one of three emails successfully - El Forum - 09-21-2009

[eluser]BrandonDurham[/eluser]
I have an email helper set up that sends the contents of a submitted form to three different email addresses. One to my own personal email address (@amillionthingstodo.com) and two to my employer's email addresses (@adlucent.com). For some reason the two to my employer's addresses never actually arrive. Stranger yet, if I enter my own email address into contact form I quickly get a MAILER-DAEMON failure notice in my inbox that says the message wasn't delivered.

Any insight as to why this might be happening??

Here is the code for my helper:
Code:
function send_comment($fromname=NULL, $fromemail=NULL, $phone=NULL, $company=NULL, $comments=NULL)
    {
        if ($fromname == NULL || $fromemail == NULL || $phone == NULL) return false;

        $CI =& get_instance();

        $CI->load->library('email');

        $CI->email->from($fromemail, $fromname);
        $CI->email->to('[email protected]');
        $CI->email->to('[email protected]');
        $CI->email->bcc('[email protected]');

        $CI->email->subject('SITE CONTACT');

        $message = "Name: " . $fromname;
        $message .= "\nEmail: " . $fromemail;
        $message .= "\nPhone: " . $phone;
        $message .= "\nCompany: " . $company;
        $message .= "\nDate & Time Sent: " . date("l, F jS (g:iA)", strtotime(date('Y-m-d H:i:s')));
        $message .= "\n===========================================================================";
        $message .= "\n\n" . $comments;

        $CI->email->message($message);

        if ($CI->email->send()) return true;
        return false;
    }



Email Helper only sends one of three emails successfully - El Forum - 09-21-2009

[eluser]Myles Wakeham[/eluser]
A couple of things look a bit unusual to me. Maybe its just that I code differently than this, but you are referencing the parent as $CI. But rather than the parent loading the email library, you have the function doing it for the parent. For me, that is a bit backwards - I'd have the parent load the library, and let the function reference the loaded library by the parent. Otherwise you are just repeating the process over and over. That might be why its failing on subsequent sends. Just a thought...

And one other thing - are you absolutely sure that the problem is in the code? Depending on your SMTP server that is dispatching your emails, or the ISP that hosts it, they might be blocking outgoing emails due to some restriction.

Have you tried a simple PHP mail() command to see if that makes any difference?

Myles


Email Helper only sends one of three emails successfully - El Forum - 09-21-2009

[eluser]BrandonDurham[/eluser]
I haven't tried the simple mail() test yet.

How would you change the code above? It's strange... when testing locally (MAMP) it works perfectly, but when I upload it to the server (adlucent.com) it only send the email to my personal address.


Email Helper only sends one of three emails successfully - El Forum - 09-21-2009

[eluser]BrianDHall[/eluser]
First, try sending 2-3 emails using mail() in a row to yourself in a loop. If it works locally but not on the upload server then it is a server configuration issue more than its a code issue.

If this manual mail() works on the server then it is back into the code.

For pure code change, try this:

Quote:$this->email->clear()

Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.

Code:
foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('[email protected]');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}
If you set the parameter to TRUE any attachments will be cleared as well:

$this->email->clear(TRUE);



Email Helper only sends one of three emails successfully - El Forum - 09-21-2009

[eluser]Myles Wakeham[/eluser]
[quote author="BrandonDurham" date="1253583726"]
How would you change the code above? It's strange... when testing locally (MAMP) it works perfectly, but when I upload it to the server (adlucent.com) it only send the email to my personal address.[/quote]

Does sound like a server configuration setting issue. Some ISPs will throttle outgoing emails to protect themselves from spam. Sometimes this is done by throttling the number of outgoing emails allowed in a time period (ie. no more than 1 per 20 seconds, or something like that). Other times there is a max put per hour of total emails allowed out (ie. 300 max per hour). Depends on your hosting provider.

As for the code, what I would normally do is in the controller, I would do the load of the library. The controller then becomes the 'parent' and you would reference in your function the controller like this:

Code:
function DoSomething(){
        $contr = & get_instance();
    $email_object = $contr->email;

        $email_object->clear();
        $email_object->subject = 'This is the subject';
etc.

It doesn't really matter what the class is that you are referencing (CI classes, your own classes, etc.). If they are instantiated in the controller, you refer to the through the & get_instance() call to get a handle to where they were created. What you are doing is to load them repetitively in the function, so that each time the function is called it has to check if it needs to create a new object each time, which probably doesn't need to be done. I may be being pedantic here, but its just a coding habit that I like (I like to know where my objects are being created from).

As a previous poster said, to be absolutely sure that each new reference from load is clear, you should do some form of clear() call. This is the same regardless of where the object is being instantiated from of course. I don't know the PHP code of the email helper (I use phpMailer for my email sending) but if I remember the code for library initialization, it checks to see if the library was already loaded. If not, it loads it. If it was previously loaded, however, it just returns a handle to that object. So its entirely likely that your previous email settings are being left over each time. The clear() call is critical in that case, I would assume.

Myles