WP-Mix

A fresh mix of code snippets and tutorials

PHP Send Bulk Emails from Text File

This post provides a couple of PHP code snippets that enable you to send emails from a text file. Send in plain-text format and plain-text with embedded image.

Here are two PHP techniques for sending out mass/bulk emails using a text file that contains all of the email addresses. The first snippet is for sending plain-text email and the second snippet is for sending plain-text email with an embedded image.

Send plain-text email from text file

The following code can be used to send plain-text email based on addresses listed in a text file (with each address on its own line):

$subject = 'Email Announcement';
$headers = "From: Example Organization <email@example.com>\n" . 
		 "Reply-To: Example Organization <email@example.com>\n" . 
		 "MIME-Version: 1.0\n" . "Content-Type: text/plain; charset=iso-8859-1";

$message = 'Email message goes here..';

$emails = file('list-of-email-addresses.txt', FILE_IGNORE_NEW_LINES);
$x = 1;
foreach ($emails as $email) {
	$mail_sent = @mail($email, $subject, $message, $headers, '-f email@example.com');
	echo $mail_sent ? $x . ', ' : '0, '; 
	$x++;
}

To use this script:

  • Include this script in a PHP file and upload to your server. Then access the file via any web browser to run the script.
  • Edit the $subject, $headers, and $message with your correct/desired infos.
  • Edit list-of-email-addresses.txt to match the name/location of the text file that contains all of your email addresses in list format (one address per line).
  • You may want to beef up the script output (i.e., the echo $mail_sent line) to get a better idea of the results (currently the output is very basic).
  • Make sure to do a test first using your own test email addresses.

Also check out the Notes below for more important infos.

Send plain-text email with embedded image

This next code snippet can be used to send out mass emails that include an embedded image. So the email format is multipart/mixed, with the message formatted as plain text and the image embedded as an attachment.

$random_hash = md5(date('r', time()));

$subject  = 'Email Announcement!';
$headers  = "From: Example Organization <email@example.com>\r\nReply-To: Example Organization <email@example.com>"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

$attachment = chunk_split(base64_encode(file_get_contents('example-image.jpg'))); 

ob_start(); ?>

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Email message goes here..

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: image/jpeg; name="example-image.jpg"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
$message = ob_get_clean();
$emails = file('list-of-email-addresses.txt', FILE_IGNORE_NEW_LINES);
$x = 1;
foreach ($emails as $email) {
	$mail_sent = @mail($email, $subject, $message, $headers);
	echo $mail_sent ? $x . ', ' : '0, '; 
	$x++;
}

To use this script:

  • Include this script in a PHP file and upload to your server. Then access the file via any web browser to run the script.
  • Edit the $subject, $headers, and “Email message goes here..” with your correct/desired infos.
  • Edit list-of-email-addresses.txt to match the name/location of the text file that contains all of your email addresses in list format (one address per line).
  • Make sure that example-image.jpg exists in the same directory as the script, or edit the code to match the correct name and location of your image.
  • You may want to beef up the script output (i.e., the echo $mail_sent line) to get a better idea of the results (currently the output is very basic).
  • Make sure to do a test first using your own test email addresses.

Also check out the Notes below for more important infos.

Notes:

Sending bulk email is one of the trickiest things you can do online, so make sure to cover all of your bases. Here are some things to keep in mind:

  • Use at your own risk. Google, Hotmail, et al are notorious for flagging domains and marking just about any email as “spam”. So make sure you know what you are doing (do some research before blasting out mass emails).
  • Make sure your list of email addresses is not accessible to public! (run the script from a protected directory or site)
  • If your test emails show weird or garbled characters, try replacing iso-8859-1 with UTF-8
  • Highly recommended to check for the latest requirements for proper headers, to help ensure delivery.

Whew! That’s all there is to it. Have fun and be safe.

★ Pro Tip:

USP Pro