简体   繁体   English

Sendgrid x-smtpapi错误,“缺少目标电子邮件”

[英]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 . 问题是它不会发送到$to_emails数组中的收件人并输出“缺少目标电子邮件”。它确实输出了我传递的电子邮件的数组,但仍在数组中寻找“收件人”值-我不知道为什么,我该怎么解决,我跟着Sendgrid文档密切,据我可以看到- ? https://sendgrid.com/docs/Code_Examples/php.html

If I uncomment the 'to' line it sends the email to example@example.com just fine. 如果我取消注释“收件人”行,它将电子邮件发送到example@example.com就好了。

The to parameter is currently always required even if using the SMTPAPI to . 即使始终使用SMTPAPI to当前始终也需要to参数。 We recommend setting it to the from address. 我们建议将其设置为from地址。 If the SMTPAPI to is present, the API will ignore the regular to parameter. 如果存在SMTPAPI to ,则该API将忽略常规to参数。

The example that you link to shows both to and x-smtpapi parameters present and states 你链接到同时显示了例子tox-smtpapi参数和目前的状态

...the email will go out to both example1@sendgrid.com and example2@sendgrid.com. ...电子邮件将同时发送到example1@sendgrid.com和example2@sendgrid.com。 The normal to address, example3@sendgrid.com, will not receive an email. 普通用户example3@sendgrid.com将不会收到电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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