user1711384
user1711384

Reputation: 353

Sending a mail with php: Script is waiting for the answer?

I am using the class PHPMailer to send Mails via SMTP:

   <?php 
require 'php_mailer/class.phpmailer.php';

    $mail = new PHPMailer;

    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.dfgdfgdfg.de';              // Specify main and backup server
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'dfgdfg';                            // SMTP username
    $mail->Password = 'dfgsdfgdsfg';                           // SMTP password
    //$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

    $mail->From = '[email protected]';
    $mail->FromName = 'dfgdfgdg';
    $mail->AddAddress('[email protected]', 'Udo');  // Add a recipient
$mail->AddBCC('[email protected]');

    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->IsHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'HTML-Mail mit Logo';
    $mail->Body    = 'Nachfolgend das <b>Logo</b>';
    $mail->AltBody = 'Aktiviere HTML, damit das Logo angezeigt wird';

    if(!$mail->Send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }

?>

My Questions:

  1. Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?
  2. Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!

Thanks! Alex

Upvotes: 1

Views: 2401

Answers (2)

Techie
Techie

Reputation: 45124

Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?

You can do something like, Set the name too.

// rest of code first
$mail->AddAddress("[email protected]")

$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
  $mail->AddBCC($row[0]);
}

$mail->Send();//Sends the email

You can have special string 'name_here' in the body and place $name with str_replace function

Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!

Yes according to my knowledge you will have to wait.

How to do a str_replace ? Assume that your email body is as follows

$body = " Dear %first_name%,

other stuff goes here....... ";

$body = str_replace("%first_name%", $first_name, $body); 

above will replace %first_name% with the name($first_name) you provide.

Upvotes: 1

Palantir
Palantir

Reputation: 24182

You are setting PHPMailer to interact with SMTP, so I guess that it will wait for it to complete. This is not optimal, because as you say you will block the PHP script until SMTP responds.

It would be better to send through your localhost: set PHPMailer to use sendmail, which will usually be a wrapper to a local exim4 or postfix, which will then handle the mailing for you. This is much better, also because the local server will handle any possible temporary error, and retry later. PHP won't.

You may also want to explore other options, like Mandrill or Sendgrid to do the job, especially if you do lot of mailing or bulk mailing.

Upvotes: 2

Related Questions