sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

Sending an Email to multiple users using a single api request #269

Closed ynagarjuna2012 closed 7 years ago

ynagarjuna2012 commented 8 years ago

Issue Summary

I want to send email to 1000 users with a single api request.

I have used the personalizations addTo() multiple times but it show all the mail ID's in to field.

I don't want to let my users see other email Id's another than his email ID.

Steps to Reproduce

I am using an example which is shown on the Sendgrid Website but it constantly throwing some errors.

Code:

         require 'vendor/autoload.php'
         $sendgrid = new SendGrid( $sg_username, $sg_password );'

       $mail = new SendGrid\Email();

      /* SMTP API
       ====================================================*/
     // ADD THE RECIPIENTS
     $emails = array (
        "test@gmail.com",
        "test2@gmail.com
     );
    $mail->setTos($emails);

  try {
      $mail->setTos($emails)->
     setFrom( "community@zenparent.in" )->
      setSubject( "HEllo" )->
       setText( "Hello,\n\nThis is a test message from SendGrid.    We have sent this to you because                   you requested a test message be sent from your account.\n\nThis is a link to google.com: http://www.google.com\nThis is a link to apple.com: http://www.apple.com\nThis is a link to sendgrid.com: http://www.sendgrid.com\n\nThank you for reading this test message.\n\nLove,\nYour friends at SendGrid" )->
     setHtml( "<table style=\"border: solid 1px #000; background-color: #666; font-family: verdana, tahoma, sans-serif; color: #fff;\"> <tr> <td> <h2>Hello,</h2> <p>This is a test message from SendGrid.    We have sent this to you because you requested a test message be sent from your account.</p> <a href=\"http://www.google.com\" target=\"_blank\">This is a link to google.com</a> <p> <a href=\"http://www.apple.com\" target=\"_blank\">This is a link to apple.com</a> <p> <a href=\"http://www.sendgrid.com\" target=\"_blank\">This is a link to sendgrid.com</a> </p> <p>Thank you for reading this test message.</p> Love,<br/> Your friends at SendGrid</p> <p> <img src=\"http://cdn1.sendgrid.com/images/sendgrid-logo.png\" alt=\"SendGrid!\" /> </td> </tr> </table>" );
    $sendgrid->send( $mail );

  echo "Sent mail successfully.";
  } catch ( Exception $e ) {
  echo "Unable to send mail: ", $e->getMessage();
 }

}

The above code throwing the error. How to send emails to multiple users using Web API V3 or if it is not poosible to send through it.

Please provide the example on how to use send mail with SMTP API

thinkingserious commented 8 years ago

Hello @ynagarjuna1995,

The SMTP API functionality is now built into the v3 /mail/send endpoint.

You will want to set a personalization block for each to email.

Here is an example with 2 personalization blocks using our mail helper: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L22

Please let us know if you need additional help.

With Best Regards,

Elmer

mikegreen commented 8 years ago

Based on this: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html#-Sending-the-same-Email-to-Multiple-Recipients adding multiple To's results in "These recipients will all be able to see each other on the email."

thinkingserious commented 8 years ago

Thanks for jumping in @mikegreen!

mikegreen commented 8 years ago

Thanks - so that leaves me wondering - is there a way to dispatch multiple emails with a single "to" in one call? I didn't find an obvious way so I just make a gaggle of calls (not a problem when sending 150 emails at a time.. but thousands? eek).

ynagarjuna2012 commented 8 years ago

@mikegreen Feeling same. They don't have implementation to pass multiple emails with single to .. according to @thinkingserious we need to build 'n' personalization objects for sending 'n' different mails.

Hope Sendgrid will provide better way to implement this :)

thinkingserious commented 8 years ago

@mikegreen you can have multiple personalizations in the same call.

@ynagarjuna1995 we are definitely open to ideas on better methods. Personally, I think the personalizations route is a much better approach versus our v2 endpoint.

thinkingserious commented 7 years ago

Please follow this issue for progress. Thank you!

vishalims095 commented 3 years ago

const msg = { to: ['test@yopmail.com', 'test@gmail.com'], from: 'desk@yopmail.com', subject: subject, html: html, }; sgMail.send(msg).then(() => { }, error => { console.error(error ,'inside then'); if (error.response) { console.error(error.response.body ,'inside then if condition') } }) .catch((err) => { console.log(err, 'inside catch'); });

nyvelius commented 3 years ago

2021 checking in. :D

Was there ever a solution to this?

I tried using a Personalization in the Java library, but every address entered in the "to" field will still see every other address, which is problematic in a number of ways and likely a GDPR violation, so it's unfortunately a no go.

Any other solution? Or do we have to call the API once per email?

artur1313 commented 3 years ago

@nyvelius yes they got a solution dosc just use { to: [ { email: email, }, ], }, { to: [ { email: email, }, ], },

instead of { to: [ { email: email, }, { email: email, }, ], }

petar-ceho commented 2 years ago

Is there a way to send in one request multiple template_id,so not just to have multiple users example: { to: [ { email: email, }, ], }, { to: [ { email: email, }, ], },

but for each section of to to have a individual template_id ?

jamesvclements commented 2 years ago

Instead of explicitly passing the to addresses, is it possible to omit to and just email all contacts?