尝试使用Mailgun在Laravel 4.2中发送电子邮件时开始出现此错误。最长的时间一直可以正常工作,并且没有对代码进行任何更改。
错误:“客户端错误响应[url] https://api.mailgun.net/v2/ {domain_name} /messages.mime [状态代码] 400 [原因短语]错误请求”
config / mail.php
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => array('address' => 'info@my-domain.ca', 'name' => 'Domain Portal'),
'encryption' => 'tls',
'username' => 'postmaster@my-domain.ca',
'password' => 'xxxxxxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
config / services.php
'mailgun' => array(
'domain' => 'my-domain.ca',
'secret' => 'key-xxxxxxxx',
),
composer.json
"laravel/framework": "4.2.*",
"guzzlehttp/guzzle": "5.0.3",
controller.php
if(isset($email_data['recipient_email']) && $email_data['recipient_email']) {
Mail::send($mailView, $data, function($message) use ($email_data, $attachments)
{
$message->from($email_data['email_sender_address'], $email_data['email_sender_name'])
->to($email_data['recipient_email'], $email_data['recipient_name'])
->subject($email_data['subject']);
if ($email_data['bcc']) {
$message->bcc($email_data['bcc']);
}
if ($attachments) {
foreach ($attachments as $a) {
$message->attach($a);
}
}
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', '{"chapter_id": '.$email_data['chapter_id'].'}');
$message->getHeaders()->addTextHeader('x-mailgun-native-send', true);
});
}
这真的很奇怪,当我在本地对其进行测试时,它似乎可以正常工作,有时它会发送电子邮件,但有时我只会收到错误消息。
答案 0 :(得分:0)
原来的问题是,收件人名称中包含一些特殊字符,这破坏了邮件请求。我通过不包括receive_name而是直接将其直接发送到他们的电子邮件来解决了这个问题。