< All Topics

Recipe 3: Mailchimp alternative – Mass / Bulk emails – DIY style


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.


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.


$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
     //$salutation = trim($pieces[1] . " " . trim($pieces[1]);
     if($salutation=="") {
         $salutation = "Hello there";
  }else {
         $salutation = "Hello there";
  $salutation = "Hello $salutation ";
  sendEmail($cname, $email, $salutation);
  echo "slept for 2 seconds\n\n";

function sendEmail($cname, $email, $salutation){

    echo "sending email to: $cname:$email\n";

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    try {
        $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
        //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->Subject = getSubject();
        $mail->Body    = getBody($salutation);
        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. 
Here is the 5 minute video demonstrating my skills. I am also attaching the report you may be interested in.  
Stay well.
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.

Table of Contents