Laravel 4.2 Mail::queue 中的 getBody 为空
getBody empty in Laravel 4.2 Mail::queue
我在 Laravel 4.2 通过 Mail::queue
发送电子邮件;一切正常。我使用模板,收到的电子邮件正是我想要的。在此过程中的某个时刻,我想取回主体以将其添加到特定的 table 以用于日志目的; 没有效果。
// 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()
其中 returns null
;之前没有人发布过这个问题,所以我想知道我是否是唯一一个在处理后无法获取电子邮件本身 body
的人...
我把整个 Mail::queue
过程都交给你了,以防我在这里做错什么。
谢谢你们 ;)
注意:我正在使用 MailGun 发送电子邮件,但我认为它不会改变任何问题...
我注意到 Mail::send
不会出现此问题,并试图了解此 Mail::queue
发生了什么...
似乎正文 ($message->getBody()
) 直到队列过程的最后才 processed/available,所以不可能得到它。
我试图找到获得此 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);
});
如果有人遇到同样的问题,我认为这是一个很好的解决方案:)
我在 Laravel 4.2 通过 Mail::queue
发送电子邮件;一切正常。我使用模板,收到的电子邮件正是我想要的。在此过程中的某个时刻,我想取回主体以将其添加到特定的 table 以用于日志目的; 没有效果。
// 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()
其中 returns null
;之前没有人发布过这个问题,所以我想知道我是否是唯一一个在处理后无法获取电子邮件本身 body
的人...
我把整个 Mail::queue
过程都交给你了,以防我在这里做错什么。
谢谢你们 ;)
注意:我正在使用 MailGun 发送电子邮件,但我认为它不会改变任何问题...
我注意到 Mail::send
不会出现此问题,并试图了解此 Mail::queue
发生了什么...
似乎正文 ($message->getBody()
) 直到队列过程的最后才 processed/available,所以不可能得到它。
我试图找到获得此 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);
});
如果有人遇到同样的问题,我认为这是一个很好的解决方案:)