tags:

views:

80

answers:

1

Hi. I am creating a function to send a notification email to a user using the phpMailer lib.

 public function notify($address,$subject = '',$body = null,$mailer_options = array()) {
        try {
            $phpmailer = new PHPMailer($exceptions = true);
            $phpmailer->CharSet = 'UTF-8';
            $phpmailer->IsSMTP();
            $phpmailer->SMTPAuth = true;
            $phpmailer->SMTPKeepAlive = true;
            //$phpmailer->SMTPDebug = true;
            $phpmailer->IsHTML(true);

            $phpmailer->Host = ...
            $phpmailer->Username = ...
            $phpmailer->Password = ...
            $phpmailer->From = ...
            $phpmailer->FromName =...


            $phpmailer->AddAddress($address);
            $phpmailer->Subject = $subject;
            $phpmailer->Body = $body;

            $phpmailer->Send();
            $phpmailer->ClearAllRecipients();
}

It works fine if i just send an email or sending multiple emails inside the class. But if do

for($i=0;$i<3;$++)
{
   $notification = new $Notification();
   $notification->notify(...);
}

It retuns a blank page. No errors, messages, nothing. Before you ask i have display_errors turned on.

What can it be?

It works fine if i just have one instance of phpmailer like this:

$phpmailer = new PHPMailer($exceptions = true);
(...)

       for($i=0;$i<3;$i++)
       {
            $phpmailer->AddAddress('address');
            $phpmailer->Subject = "";
            $phpmailer->Body = "sasa";

            $phpmailer->Send();
            $phpmailer->ClearAllRecipients();
       }
+1  A: 

Remove the $ from new Notification:

for($i=0;$i<3;$++)
{
   $notification = new Notification();
   $notification->notify(...);
}

new $Notification will create a new instance from the value of variable $Notification. That would only work if $Notification really contains "Notification" (assuming your class is named "Notification")

If you've turned display_errors on in your PHP script, but the server has disabled it by default, errors won't be displayed if there is a syntax error in your script.

Lekensteyn
Sorry it was a typing error. I have adapted my code to put it here
Popovich88
I see a `try` block. Where is the `catch`? How did you enable `display_errors`? With .htaccess / php.ini or in the PHP script? See edited post.
Lekensteyn
i am working at localhost. Also i am using Symfony Framework in dev environment so it shows all the exceptions and erros by default.
Popovich88
Add something like `echo $i;` in your for-loop. Does it get displayed?
Lekensteyn
In prints correct 0,1,2. If i put the echo after the function call it only prints 0,1 so the script dies somehow,
Popovich88
You've a try/catch block in `notify` function. What happens when an exception occurs?
Lekensteyn
Nothing. There is no exeception. If i throw an exception myself it shows it shows the execption message.
Popovich88
I think your SMTP mail provider is limiting the number of login attempts at a time. Put a (static) reference to the instantiated `PHPMailer` class in your Notification class, and use this every time.
Lekensteyn