Jump to content
Larry Ullman's Book Forums

Send Multiple Emails Thru Smtp


Recommended Posts

I'm making a PHP newsletter sender, and I have multiple emails in a database.

 

Is the following code the proper way to send multiple emails?

 

So imagine that I have already done the MySQL call to retrieve all the emails, and I am now here:

require_once "Mail.php";
require_once "Mail/mime.php";
$mail = new Mail_Mime();
$mime->setTXTBody('Text email');
$mime->setHTMLBody('HTML email');
$mime->setFrom('Sample Email Sender <sample@email.com>');
$mime->setSubject($subject);
$body = $mime->get();
$headers = $mime->headers();
$smtp['host'] = 'smtp.emailserver.com';
$smtp['auth'] = true;
$smtp['username'] = 'username@email.com';
$smtp['password'] = 'password';
$mail =& Mail::factory('smtp', $smtp);
while($row = @mysqli_fetch_array($rq, MYSQLI_NUM)){$mail->send($row[0], $headers, $body);}
unset($mime, $mail);

 

You'll notice that what I did was set all the main email stuff outside of the while{} loop. Is this setup okay? Or do I need to move anything else into the while{} loop? Thanks again.

Link to comment
Share on other sites

This looks correct, although I will say that sending a large number of emails is a pretty advanced subject, and it makes a difference whether you're talking about sending a few dozen at a time or thousands.

Link to comment
Share on other sites

 Share

×
×
  • Create New...