Sendgrid - Receiver无法接收使用php发送的电子邮件

时间:2018-02-28 13:34:16

标签: php sendgrid

这是我的send_email.php

require("../sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email("Example User", "sample-email@sample.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "sample-email@sample.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = ('MY_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();

当我尝试运行" send_email.php"响应是202,这意味着它是正确的交付?当我检查我的Sendgrid活动日志时,它说" Delivered"。但是当接收者检查他/她的Gmail时,他/她没有收到任何东西。 (顺便说一句,我在localhost中测试这个)。请帮我。非常感谢你。

1 个答案:

答案 0 :(得分:0)

如果活动日志显示已发送或响应为“202”,则表示已发送。检查垃圾邮件文件夹或垃圾邮件。如果邮件过滤器意外删除了垃圾,请检查垃圾箱。在我的情况下,我自己收到的邮件很晚。但我使用班轮邮件。

include "/swift/SmtpApiHeader.php";
include "/swift/lib/swift_required.php";

$sendgrid_username = 'xxxx';
$sendgrid_password = 'xxxx';
$to = 'xxxx@xxxx.com';

$transport  = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername($sendgrid_username);
$transport->setPassword($sendgrid_password);

$mailer     = Swift_Mailer::newInstance($transport);

$message    = new Swift_Message();
$message->setTo($to);
$message->setCc("xxx@xxxx.ae",);
$message->addBcc("xxxx@xxxx.com");
$message->setFrom($to);
$message->setSubject("Reg: Pending work");

$msg = "Dear worker, Kindly update me the status of the work. Best Regards";

$message->setBody($msg);

try {
  $response = $mailer->send($message);
  print_r($response);
} catch(\Swift_TransportException $e) {
  print_r($e);
  print_r('Bad username / password');
}