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.
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 (
"[email protected]",
"[email protected]
);
$mail->setTos($emails);
try {
$mail->setTos($emails)->
setFrom( "[email protected]" )->
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
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
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."
Thanks for jumping in @mikegreen!
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).
@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 :)
@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.
Please follow this issue for progress. Thank you!
const msg = {
to: ['[email protected]', '[email protected]'],
from: '[email protected]',
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');
});
Most helpful comment
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."