phpsendgrid-api-v3bulk-email

Sendgrid email bulk couldn't send PHP


<?php
    if (isset($_POST['send']))
    {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $body = $_POST['message'];

                $data = array(
                    "personalizations" => array(
                        array(
                            "to" => array(
                                array(
                                    "email" => $email,
                                    "name" => $name
                                )
                            )
                        )
                    ) ,
                    "from" => array(
                        "email" => $sender
                    ) ,
                    "subject" => $subject,
                    "content" => array(
                        array(
                            "type" => "text/html",
                            "value" => $body
                        )
                    )
                );
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);
            curl_close($ch);
    
           
    
    }
    ?>

I'm using Sendgrid API to send emails with PHP I can send to one email at once but I need to pass the $email variable as the bulk of emails

array(
         "email" => $email,
         "name" => $name
         )


  

$email variable set the parameter as a collection of emails example: $email="abc@mail.com,info@abc.com,def@gmail.com"

How I do this?                                                                                                                                                                                                                                                                                                          


Solution

  • a) explode() email value with , to convert it to array

    b) Apply loop on this array and send mail.

    c) Make sure to check real value instead of $_POST['send']

    <?php
        if (!empty($_POST['email']) && !empty($_POST['subject']))
        {
            $emails = explode(',',$_POST['email']);
            $subject = $_POST['subject'];
            $body = $_POST['message'];
            
            foreach($emails as $email){
    
                $data = array(
                    "personalizations" => array(
                        array(
                            "to" => array(
                                array(
                                    "email" => $email,
                                    "name" => $name
                                )
                            )
                        )
                    ) ,
                    "from" => array(
                        "email" => $sender
                    ) ,
                    "subject" => $subject,
                    "content" => array(
                        array(
                            "type" => "text/html",
                            "value" => $body
                        )
                    )
                );
            
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $response = curl_exec($ch);
                curl_close($ch);
            }
               
        
        }
        ?>