Laravel 4 Laravel 4.2邮件::队列中的getBody为空

Laravel 4 Laravel 4.2邮件::队列中的getBody为空,laravel-4,mailgun,Laravel 4,Mailgun,我通过Laravel 4.2上的Mail::queue发送电子邮件;一切正常。我使用模板,收到的电子邮件正是我想要的。在这个过程中的某个时刻,我希望返回body,将其添加到一个特定的表中,以用于日志目的什么都不起作用 // We will queue the email (we could add a protection here) Mail::queue($template, $template_data, function($message) use ($email, $s

我通过Laravel 4.2上的
Mail::queue
发送电子邮件;一切正常。我使用模板,收到的电子邮件正是我想要的。在这个过程中的某个时刻,我希望返回body,将其添加到一个特定的表中,以用于日志目的什么都不起作用

    // 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();

});
这里唯一的问题是
消息->getBody()
,它返回
null
;以前没有人发布过这个问题,所以我想知道是否只有我一个人在处理邮件时无法获取邮件本身的
正文

我把整个
Mail::queue
过程都告诉了你,以防我在这里做错了什么

谢谢大家;)


注意:我正在使用MailGun发送电子邮件,我认为这不会改变问题的任何方面,因为…

我注意到问题不会出现在
邮件::发送中,并试图了解此
邮件::队列发生了什么事

似乎正文(
$message->getBody()
)直到队列进程的最后才被处理/可用,因此无法获取它

我试图找到方法来获得这个
主体
,但从技术上讲,要用这个
队列
系统获得一个干净的解决方案似乎是不可能的(注意: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);

});

如果有人遇到同样的问题,我认为这是一个很好的解决方案:)

需要在调用Mail::queue的所有位置重复此方法。看看我发现的这个方法