使用Laravel 5,如何查看Mailgun电子邮件是否已成功发送?

zec*_*ude 5 php api mailgun laravel-5

我正在使用Laravel 5中的本机Mailgun驱动程序发送电子邮件

\Mail::send('emails.notification', $data, function($message) use ($data)
{
  $message->to('name@gmail.com', 'Joe Bob')->subject("Headline here");
});
Run Code Online (Sandbox Code Playgroud)

这种运作良好,并正在接收电子邮件,但我想知道我怎样才能从Mailgun让我知道了邮件发送的响应.

我怎样才能获得这些信息?

zec*_*ude 2

我找到了解决方案,实际上非常简单。

使用 Laravel(使用Bogardo Mailgun 包):

$to_name = "Jim Bob";
$to_email = "jimbob@gmail.com";
$subject = "Howdy everyone!";

// Send the email and capture the response from the Mailgun server
$response = Mailgun::send('emails.transactional', array('body' => $body), function($message) use($to_email, $to_name, $subject)
{
  $message->to($to_email, $to_name)->subject($subject);
});

// HTTP Response Code 200 means everything worked as expected
if ($response->http_response_code === 200) {
  echo "Woohoo! The message sent!";
}
Run Code Online (Sandbox Code Playgroud)


在普通的 PHP 中(使用官方Mailgun PHP SDK):

// Config Information
$mailgun = new Mailgun("key-v876876dfsv876csd8768d876cfsd4");
$domain = "mg.mydomain.com";

// Prepare the necessary information to send the email
$send_data = array(
  'from' => $from_name . ' <' . $from_email . '>',
  'to' => $to_name . ' <' . $to_email . '>',
  'subject' => $subject,
  'html' => $html
);

// Send the email and capture the response from the Mailgun server
$response = $mailgun->sendMessage($domain, $send_data);

// HTTP Response Code 200 means everything worked as expected
if ($response->http_response_code === 200) {
  echo "Woohoo! The message sent!";
}
Run Code Online (Sandbox Code Playgroud)

查看所有 Mailgun HTTP 响应代码的列表:( https://documentation.mailgun.com/api-intro.html?highlight=401#errors )