Sendgrid php send to multiple recipients












2















I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.



function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;

$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);

$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}









share|improve this question


















  • 1





    Call the function for each email address.

    – muttonUp
    Apr 25 '17 at 21:44











  • Code sample please ?

    – Super Mario's Yoshi
    Apr 25 '17 at 21:44






  • 1





    really? loop through your addresses and call the function.

    – muttonUp
    Apr 25 '17 at 21:46
















2















I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.



function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;

$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);

$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}









share|improve this question


















  • 1





    Call the function for each email address.

    – muttonUp
    Apr 25 '17 at 21:44











  • Code sample please ?

    – Super Mario's Yoshi
    Apr 25 '17 at 21:44






  • 1





    really? loop through your addresses and call the function.

    – muttonUp
    Apr 25 '17 at 21:46














2












2








2


1






I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.



function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;

$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);

$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}









share|improve this question














I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.



function sendEmail($subject, $to, $message) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;

$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);

$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}






php sendgrid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 25 '17 at 18:28









Super Mario's YoshiSuper Mario's Yoshi

5911919




5911919








  • 1





    Call the function for each email address.

    – muttonUp
    Apr 25 '17 at 21:44











  • Code sample please ?

    – Super Mario's Yoshi
    Apr 25 '17 at 21:44






  • 1





    really? loop through your addresses and call the function.

    – muttonUp
    Apr 25 '17 at 21:46














  • 1





    Call the function for each email address.

    – muttonUp
    Apr 25 '17 at 21:44











  • Code sample please ?

    – Super Mario's Yoshi
    Apr 25 '17 at 21:44






  • 1





    really? loop through your addresses and call the function.

    – muttonUp
    Apr 25 '17 at 21:46








1




1





Call the function for each email address.

– muttonUp
Apr 25 '17 at 21:44





Call the function for each email address.

– muttonUp
Apr 25 '17 at 21:44













Code sample please ?

– Super Mario's Yoshi
Apr 25 '17 at 21:44





Code sample please ?

– Super Mario's Yoshi
Apr 25 '17 at 21:44




1




1





really? loop through your addresses and call the function.

– muttonUp
Apr 25 '17 at 21:46





really? loop through your addresses and call the function.

– muttonUp
Apr 25 '17 at 21:46












3 Answers
3






active

oldest

votes


















4














The SendGridMail class supports adding multiple to addresses through the SendGridPersonalization class.



You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35



Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail object, must have at least one Personalization.



Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958



You can create a Mail object without this and later add your own Personalization.






share|improve this answer


























  • Thank you that helped me to do this, i pasted my final code below.

    – Super Mario's Yoshi
    Apr 28 '17 at 19:41






  • 1





    @SuperMario'sYoshi You should accept this answer if it helped you.

    – ceejayoz
    Apr 28 '17 at 19:42



















2














At the end this is how i managed to do this and it's working good.



function sendEmail($subject, $to, $message, $cc) {
$from = new SendGridEmail(null, "sample@email.com");
$subject = $subject;

$to = new SendGridEmail(null, $to);
$content = new SendGridContent("text/html", $message);
$mail = new SendGridMail($from, $subject, $to, $content);

foreach ($cc as $value) {
$to = new SendGridEmail(null, $value);
$mail->personalization[0]->addCC($to);
}

$apiKey = 'MY_KEY';
$sg = new SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();


}






share|improve this answer































    2














    function makeEmail($to_emails = array(),$from_email,$subject,$body) {
    $from = new SendGridEmail(null, $from_email);

    $to = new SendGridEmail(null, $to_emails[0]);
    $content = new SendGridContent("text/plain", $body);
    $mail = new SendGridMail($from, $subject, $to, $content);
    $to = new SendGridEmail(null, $to_emails[1]);
    $mail->personalization[0]->addTo($to);

    return $mail;
    }

    function sendMail($to = array(),$from,$subject,$body) {

    $apiKey = 'your api key';
    $sg = new SendGrid($apiKey);
    $request_body = makeEmail($to ,$from,$subject,$body);
    $response = $sg->client->mail()->send()->post($request_body);
    echo $response->statusCode();
    echo $response->body();
    print_r($response->headers());
    }

    $to = array('test1@example.com','test2@example.com');
    $from = 'from@example.com';
    $subject = "Test Email Subject";
    $body = "Send Multiple Person";

    sendMail($to ,$from,$subject,$body);





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43618340%2fsendgrid-php-send-to-multiple-recipients%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      The SendGridMail class supports adding multiple to addresses through the SendGridPersonalization class.



      You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35



      Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail object, must have at least one Personalization.



      Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958



      You can create a Mail object without this and later add your own Personalization.






      share|improve this answer


























      • Thank you that helped me to do this, i pasted my final code below.

        – Super Mario's Yoshi
        Apr 28 '17 at 19:41






      • 1





        @SuperMario'sYoshi You should accept this answer if it helped you.

        – ceejayoz
        Apr 28 '17 at 19:42
















      4














      The SendGridMail class supports adding multiple to addresses through the SendGridPersonalization class.



      You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35



      Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail object, must have at least one Personalization.



      Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958



      You can create a Mail object without this and later add your own Personalization.






      share|improve this answer


























      • Thank you that helped me to do this, i pasted my final code below.

        – Super Mario's Yoshi
        Apr 28 '17 at 19:41






      • 1





        @SuperMario'sYoshi You should accept this answer if it helped you.

        – ceejayoz
        Apr 28 '17 at 19:42














      4












      4








      4







      The SendGridMail class supports adding multiple to addresses through the SendGridPersonalization class.



      You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35



      Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail object, must have at least one Personalization.



      Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958



      You can create a Mail object without this and later add your own Personalization.






      share|improve this answer















      The SendGridMail class supports adding multiple to addresses through the SendGridPersonalization class.



      You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35



      Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each SendgridMail object, must have at least one Personalization.



      Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958



      You can create a Mail object without this and later add your own Personalization.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 27 '17 at 11:58

























      answered Apr 27 '17 at 9:32









      Sebastian-Laurenţiu PlesciucSebastian-Laurenţiu Plesciuc

      1,4041530




      1,4041530













      • Thank you that helped me to do this, i pasted my final code below.

        – Super Mario's Yoshi
        Apr 28 '17 at 19:41






      • 1





        @SuperMario'sYoshi You should accept this answer if it helped you.

        – ceejayoz
        Apr 28 '17 at 19:42



















      • Thank you that helped me to do this, i pasted my final code below.

        – Super Mario's Yoshi
        Apr 28 '17 at 19:41






      • 1





        @SuperMario'sYoshi You should accept this answer if it helped you.

        – ceejayoz
        Apr 28 '17 at 19:42

















      Thank you that helped me to do this, i pasted my final code below.

      – Super Mario's Yoshi
      Apr 28 '17 at 19:41





      Thank you that helped me to do this, i pasted my final code below.

      – Super Mario's Yoshi
      Apr 28 '17 at 19:41




      1




      1





      @SuperMario'sYoshi You should accept this answer if it helped you.

      – ceejayoz
      Apr 28 '17 at 19:42





      @SuperMario'sYoshi You should accept this answer if it helped you.

      – ceejayoz
      Apr 28 '17 at 19:42













      2














      At the end this is how i managed to do this and it's working good.



      function sendEmail($subject, $to, $message, $cc) {
      $from = new SendGridEmail(null, "sample@email.com");
      $subject = $subject;

      $to = new SendGridEmail(null, $to);
      $content = new SendGridContent("text/html", $message);
      $mail = new SendGridMail($from, $subject, $to, $content);

      foreach ($cc as $value) {
      $to = new SendGridEmail(null, $value);
      $mail->personalization[0]->addCC($to);
      }

      $apiKey = 'MY_KEY';
      $sg = new SendGrid($apiKey);

      $response = $sg->client->mail()->send()->post($mail);
      echo $response->statusCode();


      }






      share|improve this answer




























        2














        At the end this is how i managed to do this and it's working good.



        function sendEmail($subject, $to, $message, $cc) {
        $from = new SendGridEmail(null, "sample@email.com");
        $subject = $subject;

        $to = new SendGridEmail(null, $to);
        $content = new SendGridContent("text/html", $message);
        $mail = new SendGridMail($from, $subject, $to, $content);

        foreach ($cc as $value) {
        $to = new SendGridEmail(null, $value);
        $mail->personalization[0]->addCC($to);
        }

        $apiKey = 'MY_KEY';
        $sg = new SendGrid($apiKey);

        $response = $sg->client->mail()->send()->post($mail);
        echo $response->statusCode();


        }






        share|improve this answer


























          2












          2








          2







          At the end this is how i managed to do this and it's working good.



          function sendEmail($subject, $to, $message, $cc) {
          $from = new SendGridEmail(null, "sample@email.com");
          $subject = $subject;

          $to = new SendGridEmail(null, $to);
          $content = new SendGridContent("text/html", $message);
          $mail = new SendGridMail($from, $subject, $to, $content);

          foreach ($cc as $value) {
          $to = new SendGridEmail(null, $value);
          $mail->personalization[0]->addCC($to);
          }

          $apiKey = 'MY_KEY';
          $sg = new SendGrid($apiKey);

          $response = $sg->client->mail()->send()->post($mail);
          echo $response->statusCode();


          }






          share|improve this answer













          At the end this is how i managed to do this and it's working good.



          function sendEmail($subject, $to, $message, $cc) {
          $from = new SendGridEmail(null, "sample@email.com");
          $subject = $subject;

          $to = new SendGridEmail(null, $to);
          $content = new SendGridContent("text/html", $message);
          $mail = new SendGridMail($from, $subject, $to, $content);

          foreach ($cc as $value) {
          $to = new SendGridEmail(null, $value);
          $mail->personalization[0]->addCC($to);
          }

          $apiKey = 'MY_KEY';
          $sg = new SendGrid($apiKey);

          $response = $sg->client->mail()->send()->post($mail);
          echo $response->statusCode();


          }







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 28 '17 at 19:39









          Super Mario's YoshiSuper Mario's Yoshi

          5911919




          5911919























              2














              function makeEmail($to_emails = array(),$from_email,$subject,$body) {
              $from = new SendGridEmail(null, $from_email);

              $to = new SendGridEmail(null, $to_emails[0]);
              $content = new SendGridContent("text/plain", $body);
              $mail = new SendGridMail($from, $subject, $to, $content);
              $to = new SendGridEmail(null, $to_emails[1]);
              $mail->personalization[0]->addTo($to);

              return $mail;
              }

              function sendMail($to = array(),$from,$subject,$body) {

              $apiKey = 'your api key';
              $sg = new SendGrid($apiKey);
              $request_body = makeEmail($to ,$from,$subject,$body);
              $response = $sg->client->mail()->send()->post($request_body);
              echo $response->statusCode();
              echo $response->body();
              print_r($response->headers());
              }

              $to = array('test1@example.com','test2@example.com');
              $from = 'from@example.com';
              $subject = "Test Email Subject";
              $body = "Send Multiple Person";

              sendMail($to ,$from,$subject,$body);





              share|improve this answer




























                2














                function makeEmail($to_emails = array(),$from_email,$subject,$body) {
                $from = new SendGridEmail(null, $from_email);

                $to = new SendGridEmail(null, $to_emails[0]);
                $content = new SendGridContent("text/plain", $body);
                $mail = new SendGridMail($from, $subject, $to, $content);
                $to = new SendGridEmail(null, $to_emails[1]);
                $mail->personalization[0]->addTo($to);

                return $mail;
                }

                function sendMail($to = array(),$from,$subject,$body) {

                $apiKey = 'your api key';
                $sg = new SendGrid($apiKey);
                $request_body = makeEmail($to ,$from,$subject,$body);
                $response = $sg->client->mail()->send()->post($request_body);
                echo $response->statusCode();
                echo $response->body();
                print_r($response->headers());
                }

                $to = array('test1@example.com','test2@example.com');
                $from = 'from@example.com';
                $subject = "Test Email Subject";
                $body = "Send Multiple Person";

                sendMail($to ,$from,$subject,$body);





                share|improve this answer


























                  2












                  2








                  2







                  function makeEmail($to_emails = array(),$from_email,$subject,$body) {
                  $from = new SendGridEmail(null, $from_email);

                  $to = new SendGridEmail(null, $to_emails[0]);
                  $content = new SendGridContent("text/plain", $body);
                  $mail = new SendGridMail($from, $subject, $to, $content);
                  $to = new SendGridEmail(null, $to_emails[1]);
                  $mail->personalization[0]->addTo($to);

                  return $mail;
                  }

                  function sendMail($to = array(),$from,$subject,$body) {

                  $apiKey = 'your api key';
                  $sg = new SendGrid($apiKey);
                  $request_body = makeEmail($to ,$from,$subject,$body);
                  $response = $sg->client->mail()->send()->post($request_body);
                  echo $response->statusCode();
                  echo $response->body();
                  print_r($response->headers());
                  }

                  $to = array('test1@example.com','test2@example.com');
                  $from = 'from@example.com';
                  $subject = "Test Email Subject";
                  $body = "Send Multiple Person";

                  sendMail($to ,$from,$subject,$body);





                  share|improve this answer













                  function makeEmail($to_emails = array(),$from_email,$subject,$body) {
                  $from = new SendGridEmail(null, $from_email);

                  $to = new SendGridEmail(null, $to_emails[0]);
                  $content = new SendGridContent("text/plain", $body);
                  $mail = new SendGridMail($from, $subject, $to, $content);
                  $to = new SendGridEmail(null, $to_emails[1]);
                  $mail->personalization[0]->addTo($to);

                  return $mail;
                  }

                  function sendMail($to = array(),$from,$subject,$body) {

                  $apiKey = 'your api key';
                  $sg = new SendGrid($apiKey);
                  $request_body = makeEmail($to ,$from,$subject,$body);
                  $response = $sg->client->mail()->send()->post($request_body);
                  echo $response->statusCode();
                  echo $response->body();
                  print_r($response->headers());
                  }

                  $to = array('test1@example.com','test2@example.com');
                  $from = 'from@example.com';
                  $subject = "Test Email Subject";
                  $body = "Send Multiple Person";

                  sendMail($to ,$from,$subject,$body);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 14 '17 at 14:24









                  Nanhe KumarNanhe Kumar

                  9,13934744




                  9,13934744






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43618340%2fsendgrid-php-send-to-multiple-recipients%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks