Magento 2 Send bulk emails using SendGrid API

Here in this post, I’m going to show how you can send bulk transactional emails programmatically using SendGrid API & an email template created in Magento.

Before getting started, let’s understand the used table’s role in this script.

customer_data_live => here we have a list of customers’ emails with an email sent flag.

block_email => here we have a list of customers who are blocked from sending emails.

 

<?php
ini_set("memory_limit", "-1");
ini_set('max_execution_time', 18000);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
include '/data/html/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

shootMail($connection);

function  shootMail($connection){
    $sql  = "select `template_text` from `email_template` where template_id =10";  //get you email template id from admin
    $email_template =  $connection->fetchOne($sql);

    $customer_emails = getCustomerList($connection);
    foreach  ($customer_emails as $email)
    {
        $customer_email = $email['email']; 
        $entity_id = $email['entity_id']; 
        $notblocklisted = checkifblocklisted($connection,$customer_email);   //ignore this if you have no any blocked emails record in any custom table
        if($notblocklisted==false)
        {
            $response  = sendEmail($connection,$customer_email,$email_template);
            $sql  = "update `customer_data_live` set `mail_sent` ='$response' where entity_id ='$entity_id'";
            $connection->query($sql);
        } else 
        {
            $sql  = "update `customer_data_live` set `mail_sent` ='blocklisted_email' where entity_id ='$entity_id'";
            $connection->query($sql);
        }
    }

}

function sendEmail($connection,$customer_email,$message_text)
{
    $response_mesage=NULL;
    $subject="Celebrating the Sale"; 
    $url = 'https://api.sendgrid.com/';
    $user = 'example@gmail.com';
    $pass = 'test@132';     
    $json_string = array(   
        'category' =>'no-reply'    
    );  

    $fromaddress='Sales'; 
    $from='sales@example.com';
    $request = $url . 'api/mail.send.json';
    $html = $message_text;
    $html = urlencode($html);             
    $session = curl_init($request);   
            
    curl_setopt($session, CURLOPT_POSTFIELDS, "api_user=".$user."&api_key=".$pass."&x-smtpapi=".json_encode($json_string)."&X-PM-TrackOpens=true&to[]=$customer_email&subject=".$subject."&html=".$html."&fromname=".$fromaddress."&from=$from");curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = json_decode(curl_exec($session),true);
    if(!empty($response) &&  is_array($response))
    {
        $response_mesage = $response['message']; 

    }
    curl_close($session);   
    return  $response_mesage;       

}

function getCustomerList($connection){
    $sql =  "SELECT `email`,`entity_id` FROM `customer_data_live` where  mail_sent is  null limit 20";
    return $connection->fetchAll($sql);
}

function  checkifblocklisted($connection,$email){
    $sql  = "select email from `block_email` where email ='$email'";
    $email =  $connection->fetchOne($sql);
    if(!empty($email))
    {
        return true;
    }
    return false;
}

 

 

 

Leave a Reply