Recipe 3: Mailchimp alternative – Mass / Bulk emails – DIY style
Introduction
Sometimes you just want to save time and money while sending hundreds of personalized emails. If you just need to send hundreds of emails per day then this do-it-yourself approach fits the bill.
Yes, your emails could be flagged as spam but most if not all emails will still get through. Note that this recipe will work on hundreds of emails sent per day since most email service providers have a daily limit. The solution you will see below can handle sending attachments as well as links as part of the email. For best results, you may wish to use basic text with no links.
The final recipe will also have the ability to personalize emails by addressing the recipient by their last name.
Prerequisites
Before we begin, here are the prerequisites before you can run the script
- Make sure you have PHP installed on your system
- Download & Install composer
(e.g. https://getcomposer.org/download/) - Go to command prompt
- Create a new folder where you want to store the email script
- Go to the newly created folder
- Install PHPMailer PHP module by running the following command:
composer require phpmailer/phpmailer
The Script
Next, create the following script in your favourite text editor. Call the file “sendemail-list.php”. Note that I am using outlook.com settings as an example but feel free to change it to your email provider.
<?php require("./vendor/phpmailer/src/PHPMailer.php"); require("./vendor/phpmailer/src/Exception.php"); require("./vendor/phpmailer/src/SMTP.php"); $file = fopen('emaillist.csv', 'r'); //Main loop: go through each email recepient and send email one by one while (($line = fgetcsv($file)) !== FALSE) { $cname = trim($line[2]); $email = trim($line[3]); $pieces = explode(" ", $cname); $salutation = $cname; //handle salutations if(isset($pieces[1])){ //$salutation = trim($pieces[1] . " " . trim($pieces[1]); if($salutation=="") { $salutation = "Hello there"; } }else { $salutation = "Hello there"; } $salutation = "Hello $salutation "; sendEmail($cname, $email, $salutation); sleep(2); echo "slept for 2 seconds\n\n"; } fclose($file); function sendEmail($cname, $email, $salutation){ echo "sending email to: $cname:$email\n"; $mail = new PHPMailer\PHPMailer\PHPMailer(); try { $mail->isSMTP(); $mail->Host = "smtp.live.com"; $mail->SMTPAuth = true; $mail->Username = "your-address@outlook.com"; //Password to use for SMTP authentication $mail->Password = "YOUR PASSWORD"; $mail->SMTPSecure = 'tls'; $mail->Port = 587; //smtp port $mail->SMTPDebug = 2; $mail->setFrom('your-address@outlook.com', 'YOUR NAME'); //to name and email $mail->addAddress($email); //send a copy to yourself $mail->addBCC("your-address@outlook.com", "Your Name"); //add file attachement - can comment out if not sending attachement $mail->addAttachment(__DIR__ . '\attachement.txt'); //enable html mode $mail->isHTML(true); $mail->Subject = getSubject(); $mail->Body = getBody($salutation); $mail->send(); echo $mail->Body; echo 'Message has been sent\n\n'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo; } } function getSubject(){ return "Interested in your program"; } function getBody($salutation){ return "$salutation, I hope this email finds you well. I am very much interested in your program. <br><br> Here is the 5 minute video demonstrating my skills. I am also attaching the report you may be interested in. <br><br> https://www.youtube.com/somevideo <br><br> Stay well. <br><br> Sincerely,<br> My Name<br> My Signature<br> "; } ?>
Note that you will need to update the script to reflect your settings (e.g. your email account information, emaillist.csv text file with your email recipient list etc..).
Email Recipient File
In order for the script to run you will also need to create your email recipient csv/text file. Here is an example content
1,My Friend A,Mr. Smith,some-address@hotmail.com,Product A,, 2,My Friend B,Ms. Smith,some-address@gmail.com,Product B,,
I am only using fields 3 & 4 (e.g. salutation/name of the recipient and their email address). Other fields are there to give you ideas for additional email personalizations.
Running the Script
Once you are done with those changes, to run your email campaign run the script as follows:
php sendemail-list.php
There you have it!
If you are on a Mac, the script should work with little or no adjustments. If you get errors and you are using one of the big email providers you may need to enable special programmatic access to get it to work.
You can download the full recipe using this link.
Happy programming.