5

I am using this script to send out email via cURL. I am not using the sendgrid library and I already looked at the API docs. I would like the option to send to multiple "to" addresses. How can I properly do this?

$params = array(
    'to'        => $to,   
    'subject'   => $title,
    'text'      => 'Subject',
    'from'      => 'mail@mail.com',
);

$request =  $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$pass;

$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// add authorization header
curl_setopt($session, CURLOPT_HTTPHEADER,$headr);

$response = curl_exec($session);
curl_close($session);
bbglazer
  • 123
  • 3
  • 16
  • 1
    This is not a question about `cURL` or php, you have to take a look into the documentation of that API you are using. We cannot magically _guess_ what that API allows or expects. – arkascha Oct 22 '16 at 19:27
  • I'm not using their php library. – bbglazer Oct 23 '16 at 05:49
  • 1
    Especially if you do not use their implementation: you need to know how the expect the request yiu send should look like, shiuld be structured. Only they can answer that. Only they know what they try to interpret how. – arkascha Oct 23 '16 at 10:34

6 Answers6

2

Try

    $params = array(
    'to[0]'        => $to1,   
    'to[1]'        => $to2,
    );

it was worked for me.

1

Look at SendGrid API documentation (e.g. for v2 API): https://sendgrid.com/docs/API_Reference/Web_API/mail.html

This can also be passed in as an array, to send to multiple locations. Example: to[]=a@mail.com&to[]=b@mail.com

So you can add this $to param as array :

$to = ["one@email.com", "two@email.com"]; // etc.
Mateusz Palichleb
  • 765
  • 12
  • 20
  • If I make $to an array with two addresses, it doesn't work. – bbglazer Oct 23 '16 at 05:08
  • What version of API do you use? My example concerns API v2. Maybe there's some differences? (if you use v3, etc) – Mateusz Palichleb Oct 23 '16 at 08:50
  • I also realised, that the `$params` array is multidimensional array in this case, so cURL have to be set with additional requirements. For example you need to add to the $headr[] the `"Content-type: multipart/form-data"` and pass your array into `http_build_query()`. Look at this question -> http://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl – Mateusz Palichleb Oct 23 '16 at 09:10
0

Here's how SendGrid does it in the documentation: Sending a Basic Email to Multiple Recipients.

In your example, the to string is the native SMTP TO: value. So you can't use an array, but you can provide a comma-separate string of multiple addresses. Each of those messages would be processed by SendGrid, and each recipient would see all the addresses of each other, in classic "conversation" style.

If you want to leverage SendGrid's "send the same email to multiple people distinctly" ability, you'd need to leverage the v3 API as above.

jacobmovingfwd
  • 2,185
  • 14
  • 11
0

For API V2 store emails in array like:

$json_string = array(
'to' => array(
'amin.charoliya@conversionbug.com', 'amincharoliya@gmail.com'
),
'category' => 'test'
);

and then add it into your $param array:

'x-smtpapi' => json_encode($json_string),

Please note that in this case the normal to address,will not receive an email. For further detail visit : https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html

Amin charoliya
  • 566
  • 1
  • 5
  • 15
0

Try this:

$params = array(
'to[0]'        => 'abc@email.com',   
'to[1]'        => 'xyz@email.com',   
'subject'   => $title,
'text'      => 'Subject',
'from'      => 'mail@mail.com',
);

 $request =  $url.'api/mail.send.json';
 $headr = array();
 // set authorization header
 $headr[] = 'Authorization: Bearer '.$pass;

 $session = curl_init($request);
 curl_setopt ($session, CURLOPT_POST, true);
 curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
 curl_setopt($session, CURLOPT_HEADER, false);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

 // add authorization header
 curl_setopt($session, CURLOPT_HTTPHEADER,$headr);

 $response = curl_exec($session);
 curl_close($session);

The only change you have to do is to add this code =>

    'to[0]'        => 'abc@email.com',   
    'to[1]'        => 'xyz@email.com', 
0

Building off of Yossi's answer, I did...

//if we have an array of email addresses, add a to[i] param for each
if (is_array($to)) {
    $i=0;
    foreach($to as $t) $params['to['.$i++.']']=$t;
//just one email, can add simply like this
} else {
    $params['to']=$to;
}
rgbflawed
  • 1,957
  • 1
  • 22
  • 28