简体   繁体   中英

Sendgrid x-smtpapi errors with 'missing destination email'

I've set up the following:

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

$file = date('YmdHis');
$fp = fopen('/var/www/mydir/files/'.$file.'.csv', 'w');

$url = 'http://sendgrid.com/';
$user = 'myuser';
$pass = 'mypass';
$to_emails = array(
  'to' => array(
    '1@example.com','2@example.com','3@example.com'
  ),
  'category' => 'Test'
);

$fileName = $file;
$filePath = dirname(__FILE__);

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'x-smtpapi' => json_encode($to_emails),
    //'to' => 'example@example.com',
    'subject'   => 'Test email',
    'html'      => '<p>Testing sendgrid mail</p>',
    'text'      => 'sent',
    'from'      => 'me@example.com',
    'files['.$file.'.csv]' => '@'.$filePath.'/files/'.$fileName.'.csv'
  );

print_r($params);

$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);

// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);

// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

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

print_r($response);

//unlink('./files/'.$file.'.csv');

mysql_close($connect);

The issue is that it doesn't send to my recipients in the $to_emails array and outputs "missing destination email'. It does output the array of the emails I pass in but it is still looking for the "to" value in the array - I'm wondering why and how I can resolve? I've followed the Sendgrid documentation closely as far as I can see - https://sendgrid.com/docs/Code_Examples/php.html .

If I uncomment the 'to' line it sends the email to example@example.com just fine.

The to parameter is currently always required even if using the SMTPAPI to . We recommend setting it to the from address. If the SMTPAPI to is present, the API will ignore the regular to parameter.

The example that you link to shows both to and x-smtpapi parameters present and states

...the email will go out to both example1@sendgrid.com and example2@sendgrid.com. The normal to address, example3@sendgrid.com, will not receive an email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM