Problems with php mailer

Hey guys I have the mailer helper class which is using PhP mailer 5.1


<?php


class Mailer {


    public function email($message, $sendTo, $subject) {

        require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';


        try {


            $mail = new PHPMailer(TRUE );

            $mail->IsSMTP();                           // tell the class to use SMTP

            $mail->SMTPDebug  = 0;

            $mail->SMTPAuth = true;                  // enable SMTP authentication

            $mail->Port = 25;                    // set the SMTP server port

            $mail->Host = "test.test.net"; // SMTP server

            $mail->Username = "test.com";      // SMTP server username

            $mail->Password = "test";            // SMTP server password

            $mail->Mailer = "smtp";

            $mail->From = 'test@test.com';

            $mail->FromName = 'Paul';

            $mail->AddAddress($sendTo);

            $mail->Subject = $subject;

            $body = $message;

            $mail->MsgHTML($body);

            $mail->IsHTML(true); // send as HTML

            $mail->Send();

        } catch (phpmailerException $e) {

            echo $e->errorMessage();

        }

    }


}


?>



Im then trying to call this in the contracts controller like the following.


if (isset($AlreadyUser))

                    foreach ($AlreadyUser as $row) {

                        $field1 = $row['Email'];

                        $firstname = $row['Firstname'];


                          $link = Yii::app()->params['LINK'];

                        //This is the message that will appear in the mail sent to the added user.

                            $message = '<b>Hi '. $firstname . ' Sent you a mail</font></b> <br>

                          . "<p><b>Message:</b></P>Hi,<br>Please view this<br><br>"

                          . '<b>Description:</b>'.$model->Description . "<br>

                          . "<br>Kind Regards,<br>The Test Team."

                          . "<br>Please go to the following page to view what i said:<br><a               href=$link>Test.com</a>";


                          //TBD - test

                          $subject = 'View me!';


                          $mailer = new Mailer();

                          $mailer->email($message, $field1, $subject);

            }



What I am trying to do is send to multiple people depending on the result of the foreach loop. However I keep getting the error

C:\xampp\htdocs\My_Project\protected\extensions\PHPMailer_v5.1\class.phpmailer.php on line 2319

I believe this is because im trying to call the same class again however I would have thought that if 2 people were added it would go through the loop first time, set all the details and send the mail, the second time round it would have set all the details to the second user and sent a new mail with the new details. Does anyone know a fix for this or a link to a site that has a fix? Thanks for any and all help.

Anyone else having this problem or can help with it? Thanks.

What is the error?

I keep getting the following error

C:\xampp\htdocs\My_Project\protected\extensions\PHPMailer_v5.1\class.phpmailer.php on line 2319

maybe you don’t have function working correctly, try to send empty mail so your self




$to      = 'nobody@example.com';

$subject = 'the subject';

$message = 'hello';

$headers = 'From: webmaster@example.com' . "\r\n" .

    'Reply-To: webmaster@example.com' . "\r\n" .

    'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, $headers);



Hey, yeah the mail definately works, I have been using it for a while now and there are no problems when it is sending to only one person, however when there are more then 1 people involved the error is thrown. The erro I linked is the only one im getting too, aplication.log does not even show any problems.

can you write what is written in that line?

Of course.




class phpmailerException extends Exception {

  public function errorMessage() {

    $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";

    return $errorMsg;

  } //This is the line in question.

}

No one ever had this problem before?

If you continue to have problems, try this Yii extension instead:

http://www.yiiframework.com/extension/email/

Do you know if this will allow me to do what I need it to? I shall go mess around with it now but if anyone has any ideas on my above work could you et me know, Thanks :)

I use it. But I also extend the Email.php class, and override its mail method. There, I use PEAR’s Mail class to connect to an SMTP server over SSL instead of using PHP’s built in mail function.

Slightly complicated for me :) Im only doing work experience for college and have no previous experience of php or yii lol. So this is all very new to me. I dont know how I would go about doing what you said :)

PEAR is a PHP package manager. Check out http://pear.php.net/package/Mail/redirected

You could alternatively use Zend_Mail or another PHP library as well.

And this will definately work with what im looking for?

Hey well finally figured it out. In the class Mailer.php the code should look like this.




<?php


Yii::import('application.extensions.PHPMailer_v5.1.*');


class Mailer {


    private $mail;


    public function initialise() {

        try {

            require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';

            $this->mail = new PHPMailer(TRUE);

            $this->mail->IsSMTP();                           // tell the class to use SMTP

            $this->mail->SMTPDebug = 0;

            $this->mail->SMTPAuth = true;                  // enable SMTP authentication

            $this->mail->Port = 25;                    // set the SMTP server port

            $this->mail->Host = "smtp.test.net"; // SMTP server

            $this->mail->Username = "test.com";      // SMTP server username

            $this->mail->Password = "test";            // SMTP server password

            $this->mail->Mailer = "smtp";

            $this->mail->From = 'info@test.com';

            $this->mail->FromName = 'test@net.com';

        } catch (Exception $e) {

            echo $e->getTraceAsString();

        }

    }


    public function email($message, $sendTo, $subject) {

        try {

            $this->mail->AddAddress($sendTo);

            $this->mail->Subject = $subject;

            $body = $message;

            $this->mail->MsgHTML($body);

            $this->mail->IsHTML(true); // send as HTML

            $this->mail->Send();

            $this->mail->ClearAllRecipients();

        } catch (Exception $e) {

            echo $e->getTraceAsString();

        }

    }


}


?>



The original code was not working as I was trying to initialize the class more then once which gave an error. The above code seperates the functionality and the initialisation and works. Just for anyone who has the same problem.