sendgrid / sendgrid-php

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

Send the same email to multiple recipients without them seeing each other #318

Closed mithunjj closed 7 years ago

mithunjj commented 7 years ago

I'm using the official PHP library.

I've read this: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html#-Sending-the-same-Email-to-Multiple-Recipients

But, I would like to send the same email to multiple recipients without them seeing each other.

Is it possible using the official library?

Any code examples would be deeply appreciated.

Please help.

offerwise commented 7 years ago

I am having the same challenge here. Would really appreciate code example.

thinkingserious commented 7 years ago

Hello @mithunjj, @offerwise,

It sounds like this is the scenario you require.

For each request body, you would have a different personalization for each recipient, but a shared subject and content body. Here is an example where you have 2 personalizations. I hope that helps!

With Best Regards,

Elmer

mithunjj commented 7 years ago

Thanks @thinkingserious

I will test the example.

But the title of this is very confusing. I would recommend it to be Sending the same Email to Multiple Recipients without revealing each others email

kotakkreatif commented 7 years ago

Hi,

I am using this php library (V3 with cURL)

At first, I am using this code below

... $arr1['personalizations'][0]['to'][]=['name'=>'NAME 1','email'=>'xxx@xxx.xxx']; $arr1['personalizations'][0]['to'][]=['name'=>'AVOR GMail','email'=>'yyy@yyy.yyy']; $arr1['subject']='Multiple Mail with comma'; $arr1['from']=['name'=>'TEST Info','email'=>'zzz@zzz.zzz']; $arr1['content'][]=['type'=>'text/html','value'=>'Heya!']; $arr1['template_id']=_SENDGRID_TEMPLATE__; ...

The result : http://prntscr.com/cy4r63

And then accidentally I'm edit the code (line 1 and 2) From $arr1['personalizations'][0]['to'][] into $arr1['personalizations'][]['to'][]

... $arr1['personalizations'][]['to'][]=['name'=>'AVOR RR','email'=>'xxx@xxx.xxx']; $arr1['personalizations'][]['to'][]=['name'=>'AVOR GMail','email'=>'yyy@yyy.yyy']; $arr1['subject']='Multiple Mail with comma'; $arr1['from']=['name'=>'TEST Info','email'=>'zzz@zzz.zzz']; $arr1['content'][]=['type'=>'text/html','value'=>'Heya!']; $arr1['template_id']=_SENDGRID_TEMPLATE__; ...

It have same result with non cURL script

... $personalization1=new SendGrid\Personalization(); $to1=new SendGrid\Email('AVOR RR','xxx@xxx.xxx'); $personalization1->addTo($to1);

$personalization2=new SendGrid\Personalization(); $to2=new SendGrid\Email('AVOR GMail','yyy@yyy.yyy'); $personalization2->addTo($to2);

$content=new SendGrid\Content('text/html','some text here');

$mail=new SendGrid\Mail(); $mail->setFrom($from); $mail->setSubject($subject); $mail->addContent($content); $mail->addPersonalization($personalization1); $mail->addPersonalization($personalization2); $mail->setTemplateId(_SENDGRID_TEMPLATE__); ...

And the results like this : http://prntscr.com/cy4son and the second email : http://prntscr.com/cy4swe Hope this can help someone

Regards

offerwise commented 7 years ago

Elmer,

That helps, but is there a way to send the same email personalized with unique name and a link substitution to each recipients, without having to make 10,000 api send calls to send grid. Basically sending in an array of emails and substitutions on a single API send call and then have your servers sort out the individual sends.

Example:

Email 1: Hello Rob

Click on this link: http://example.com/rob

Email 2: Hello Steve

Click on this link: http://example.com/steve

Etc.

Does that make sense?

From: Elmer Thomas notifications@github.com Reply-To: sendgrid/sendgrid-php reply@reply.github.com Date: Friday, October 21, 2016 at 11:12 AM To: sendgrid/sendgrid-php sendgrid-php@noreply.github.com Cc: Rob Dolafi rob@offerwise.com, Mention mention@noreply.github.com Subject: Re: [sendgrid/sendgrid-php] Send the same email to multiple recipients without them seeing each other (#318)

Hello @mithunjjhttps://github.com/mithunjj, @offerwisehttps://github.com/offerwise,

It sounds like thishttps://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html#-Sending-Two-Different-Emails-to-Two-Different-Groups-of-Recipients is the scenario you require.

For each request body, you would have a different personalization for each recipient, but a shared subject and content body. Here is an examplehttps://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L22 where you have 2 personalizations. I hope that helps!

With Best Regards,

Elmer

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/sendgrid/sendgrid-php/issues/318#issuecomment-255372659, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAFejc1mYmDUEvrvip69EKpN642Emonxks5q2LongaJpZM4Kc3SG.

thinkingserious commented 7 years ago

@offerwise,

Each email would have it's own personalization block, which would have a substitution object to handle the differing salutations and links. Similar to what you see here, except there would be multiple personalization objects.

@wongalus,

Thanks for sharing your solution, please take a moment to fill out this form so that we may send you some swag!

mithunjj commented 7 years ago

@wongalus The code is working. Thanks

Here is the sample code that I'm using to send to multiple recipients.

$recipients = array('1@example.com', '2@example.com');        
$personalization = array();
$recp_cnt = count($recipients);

        for ($i = 0; $i < $recp_cnt; $i++) {
            $personalization[$i] = new SendGrid\Personalization();
            $recipient = new SendGrid\Email(NULL, $recipients[$i]);
            $personalization[$i]->addTo($recipient);
        }

        for ($i = 0; $i < $recp_cnt; $i++) {
            $mail->addPersonalization($personalization[$i]);
        }

But there is one problem left, if I set a CC/BCC using addCc or addBcc method for the same email, its not working.

thinkingserious commented 7 years ago

Hi @mithunjj,

Could you please provide a bit more detail? Can you share the code that is not working as expected?

kotakkreatif commented 7 years ago

@mithunjj

I think the main problem is Sendgrid cannot send email with duplicated items inside personalization

All of the recipients in a single personalization object (either in the to, cc, or bcc fields), will see exactly the same email, as defined by the data in that personalization, as such we do not allow duplicate emails between these three arrays in a single personalization.

From : _https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html_

You must use very different Name and email address for every personalization.

This is my working code

$tos=[
    [null,'test1@gmail.com'],
    [null,'test2@gmail.com'],
    [null,'test3@gmail.com']
];
$ccs=[
    [null,'xxx@xxx.com'],
    [null,'yyy@yyy.com'],
    [null,'zzz@zzz.com']
];
$bccs=[
    [null,'aaa@aaa.com'],
    [null,'bbb@bbb.com'],
    [null,'ccc@ccc.com']
];

foreach($tos as $key=>$val){
    $personalization=new SendGrid\Personalization();
    $to=new SendGrid\Email($val[0],$val[1]);
    $cc=new SendGrid\Email($ccs[$key][0],$ccs[$key][1]);
    $bcc=new SendGrid\Email($bccs[$key][0],$bccs[$key][1]);

    $personalization->addTo($to);
    $personalization->addCc($cc);
    $personalization->addBcc($bcc);
    $mail->addPersonalization($personalization);
}

The result : http://prntscr.com/d1yp2s

mithunjj commented 7 years ago

@wongalus Your code is working great!

Thanks :-)

thinkingserious commented 7 years ago

Thanks for taking the time to share the solution @wongalus. We would love to send some swag your way, do you mind filling out this form?

morazain commented 6 years ago

I have created a php test program which sends out the an email to multiple recipients, personalized by recipient. It's attached for your perusal. Hope you find it useful! test3m.zip

thinkingserious commented 6 years ago

Thank you @morazain!

Would you consider making a PR to this file with your example?

With Best Regards,

Elmer

morazain commented 6 years ago

I would be happy to, but not sure as to how to go about it.

​Is a PR a Pull Request?

thinkingserious commented 6 years ago

Hi @morazain,

Yes, PR means Pull Request in this context.

If you are new to contributing on GitHub, I highly recommend this short free course.

With Best Regards,

Elmer

morazain commented 6 years ago

Ok I created the pull request

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Wed, Dec 20, 2017 at 1:34 PM, Elmer Thomas notifications@github.com wrote:

Thank you @morazain https://github.com/morazain!

Would you consider making a PR to this file https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md with your example?

With Best Regards,

Elmer

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-php/issues/318#issuecomment-353188681, or mute the thread https://github.com/notifications/unsubscribe-auth/AFo_MRSn1siuw4K9ZCf2-prpkpze_lGVks5tCX1cgaJpZM4Kc3SG .

morazain commented 6 years ago

I'm new to contributing and thought I had created it, but missed a step. The pull request has now been submitted.

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Fri, Dec 22, 2017 at 5:08 PM, Guy Morazain guy@morazain.com wrote:

Ok I created the pull request

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Wed, Dec 20, 2017 at 1:34 PM, Elmer Thomas notifications@github.com wrote:

Thank you @morazain https://github.com/morazain!

Would you consider making a PR to this file https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md with your example?

With Best Regards,

Elmer

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-php/issues/318#issuecomment-353188681, or mute the thread https://github.com/notifications/unsubscribe-auth/AFo_MRSn1siuw4K9ZCf2-prpkpze_lGVks5tCX1cgaJpZM4Kc3SG .

morazain commented 6 years ago

Elmer,

I think the Pull Request #569 has been successfully created but there appears to be one issue. One check failed:

continuous-integration/travis-ci/pr — The Travis CI build failed

​Am I doing something wrong?

​Regards,

Guy​

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Sat, Dec 23, 2017 at 10:56 AM, Guy Morazain guy@morazain.com wrote:

I'm new to contributing and thought I had created it, but missed a step. The pull request has now been submitted.

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Fri, Dec 22, 2017 at 5:08 PM, Guy Morazain guy@morazain.com wrote:

Ok I created the pull request

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Wed, Dec 20, 2017 at 1:34 PM, Elmer Thomas notifications@github.com wrote:

Thank you @morazain https://github.com/morazain!

Would you consider making a PR to this file https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md with your example?

With Best Regards,

Elmer

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-php/issues/318#issuecomment-353188681, or mute the thread https://github.com/notifications/unsubscribe-auth/AFo_MRSn1siuw4K9ZCf2-prpkpze_lGVks5tCX1cgaJpZM4Kc3SG .

thinkingserious commented 6 years ago

@morazain,

No worries regarding the Travis CI build. The PR looks good and is on our backlog for final review.

Thanks!

morazain commented 6 years ago

Any progress on reviewing my PR 569?

Cheers,

Guy Morazain Follow Me http://twitter.com/GuyMorazain

On Tue, Jan 2, 2018 at 1:05 PM, Elmer Thomas notifications@github.com wrote:

@morazain https://github.com/morazain,

No worries regarding the Travis CI build. The PR looks good and is on our backlog for final review.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-php/issues/318#issuecomment-354874970, or mute the thread https://github.com/notifications/unsubscribe-auth/AFo_MRpLbaPc3NeNzQq2K7hSfuGwx5HKks5tGpoNgaJpZM4Kc3SG .

thinkingserious commented 6 years ago

Not yet @morazain, but soon :)

loretoparisi commented 3 years ago

I have slightly modified the send_test_email in the current plugin to support multiple email recipients, as per the comments above, so posting here maybe it could be useful to someone else:

                // check headers
        $headers = $params['sendgrid_headers'];

        // validate headers
        if ( ! self::valid_emails_in_headers( $headers ) ) {
            return array(
                'message' => 'One or more email addresses in field "headers" are not valid.',
                'status' => 'error',
                'error_type' => 'sending'
            );
        }

        // void email
        $mail = new \SendGrid\Mail();

        // subject
        $subject = stripslashes( $params['sendgrid_subj'] );
        $mail->setSubject($subject);

        // body
        $body    = stripslashes( $params['sendgrid_body'] );
        if ( preg_match( '/content-type:\s*text\/html/i', $headers ) ) {
            $body_br = nl2br( $body );
        } else {
            $body_br = $body;
        }
        $content = new \SendGrid\Content("text/html", $body_br);
        $mail->addContent($content);

        // from
        $from = new \SendGrid\Email(SG_FROM_NAME, SG_FROM_EMAIL);
        $mail->setFrom($from);

        // get recipients emails - from comma separated list $to
        $to = $params['sendgrid_to'];
        $recipients = explode(',', $to);
        $recpCount = count($recipients);

        for ($i = 0; $i < $recpCount; $i++) {
            $personalization = new \SendGrid\Personalization();
            $recipient = new \SendGrid\Email(NULL, $recipients[$i]);
            $personalization->addTo($recipient);
            $mail->addPersonalization($personalization);
        }

        // send
        $apiKey = SG_APIKEY;
        $sg = new \SendGrid($apiKey);
        $response = $sg->client->mail()->send()->post($mail);

        if ( 202 === $response->statusCode() ) { // accepted
            return array(
                'message' => 'Email was sent.',
                'status' => 'updated'
            );
        }

        return array(
            'message' => 'Email wasn\'t sent.',
            'status' => 'error',
            'error_type' => 'sending'
        );