我在{strong> Laravel 4.2 上通过Mail::queue
发送电子邮件;一切正常。我使用模板,我收到的电子邮件正是我想要的。在这个过程中的某个时刻,我想回到身体,将它添加到特定的表中以便记录日志; 无效。
// We will queue the email (we could add a protection here)
Mail::queue($template, $template_data, function($message) use ($email, $subject, $user, $profile, $additional_mailgun_variables)
{
// We prepare the email trace
$email_trace = new EmailTrace;
$email_trace->recipient = $email;
$email_trace->subject = $subject;
$email_trace->user_id = $user->id;
$email_trace->user_profile_id = $profile->id;
$email_trace->prepared_at = date('Y-m-d H:i:s');
// We prepare the MailGun variables
$mailgun_variables = [
'user_id' => (int) $user->id,
'profile_id' => (int) $profile->id,
'email_trace_id' => (int) $email_trace->id,
];
// Is there any additional variable ?
if ($additional_mailgun_variables !== NULL) $mailgun_variables += $additional_mailgun_variables;
// We encode it
$encoded_mailgun_variables = json_encode($mailgun_variables);
// We finally send the email with all the correct headers
$message->to($email)->subject($subject);
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', $encoded_mailgun_variables);
// We get the body of the message
$email_trace->content = $message->getBody();
$email_trace->save();
});
这里唯一的问题是返回message->getBody()
的{{1}};之前没有人发布此问题所以我想知道我是否是唯一一个在处理完电子邮件时无法获得电子邮件本身null
的人...
我过去了整个body
进程,以防我在这里做错了。
谢谢你们;)
注意:我正在使用MailGun发送电子邮件,我认为它不会改变任何问题......
答案 0 :(得分:1)
我发现问题不会出现在Mail::send
上,并试图了解这个Mail::queue
发生了什么......
看起来身体($message->getBody()
)在队列流程结束之前没有处理/可用,因此无法获得它。
我试图找到获得此body
的方法,但在技术上听起来不可能有一个使用此queue
系统的干净解决方案(注意:Laravel在这里的灵活性非常糟糕。)
我设法第一次模仿Laravel处理此模板并通过Swift Message
发送的内容。我只是将其渲染为视图并将其放在$body
变量中。
// We resolve the body for the email trace logs
$body_preparation = View::make($template, $template_data);
$body = $body_preparation->render();
// We will queue the email (we could add a protection here)
Mail::queue($template, $template_data, function($message) use ($email, $subject, $body, $user, $profile, $additional_mailgun_variables)
{
// We prepare the email trace
$email_trace = new EmailTrace;
$email_trace->recipient = $email;
$email_trace->subject = $subject;
if ($user !== NULL) $email_trace->user_id = $user->id;
if ($profile !== NULL) $email_trace->user_profile_id = $profile->id;
$email_trace->prepared_at = date('Y-m-d H:i:s');
if ($profile !== NULL) $profile_id = $profile->id; else $profile_id = NULL;
if ($user !== NULL) $user_id = $user->id; else $profile_id = NULL;
$email_trace->content = $body;
$email_trace->save();
// We prepare the MailGun variables
$mailgun_variables = [
'user_id' => (int) $user_id,
'profile_id' => (int) $profile_id,
'email_trace_id' => (int) $email_trace->id,
];
// Is there any additional variable ?
if ($additional_mailgun_variables !== NULL) $mailgun_variables += $additional_mailgun_variables;
// We encode it
$encoded_mailgun_variables = json_encode($mailgun_variables);
// We finally send the email with all the correct headers
$message->to($email)->subject($subject);
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', $encoded_mailgun_variables);
});
如果有人遇到同样的问题,我认为这是一个很好的解决方案:)