显示主题和使用Mail :: to

时间:2018-06-26 10:32:10

标签: php laravel

我有以下代码,可使用Mail :: to函数发送电子邮件。但是我不了解如何使用Mail :: to函数设置消息的主题和正文。我下面的代码可以发送电子邮件,但没有主题,并且$ message也没有出现在电子邮件中。

您知道如何正确实现吗? (使用Mail :: to在电子邮件中包含主题和$ request->消息)

public function send(Request $request, $id){

    $conference = Conference::find($id);

    if($request->send_to == "participant"){
      // if is to send only 1 email the Mail::to is called directly here 
        Mail::to($request->participant_email)->send(new Notification($conference));
        return;
    }
    if($request->send_to == "all"){
        // $sendTo = query to get a set of emails to send the email
    }
    else{
        // $sendTo = query to get a another set of emails to send the email
    }

    foreach($sendTo as $user){
        $usersEmail[] = $user->email;
    }

    $message = $request->message;
    $subject = $request->subject;

    foreach ($usersEmail as $userEmail){
        Mail::to($userEmail)->send(new Notification($conference, $message));
    }
}

在“通知”类中,我有:

class Notification extends Mailable
{
    public $conference;

    public function __construct(Conference $conference)
    {
        $this->conference = $conference;
    }

    public function build()
    {
        return $this->markdown('emails.notification');
    }
}

在视图notifications.blade.php中,我有:

@component('mail::message')
# Notification relative to {{$conference->name}}

{{$message}}

Thanks,<br>
{{ config('app.name') }}
@endcomponent

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$emailData = array(
/* Email data */
'email'     => 'user@email.com',
'name'      => 'User name',
'subject'   => 'Email subject',
);

Mail::send('emails.template_name', ['emailData' => $emailData], function ($m) use ($emailData) {    // here it is a closure function, in which $emailData data is available in $m
$m->from('info@domain.com', 'Domain Name');

$m->to($emailData['email'], $emailData['name'])->subject($emailData['subject']);
});

答案 1 :(得分:0)

try 
{ 
    Mail::send('emails.contact_form', ['data' => $data], 
    function($message) use ($data)
    {
        $message->from('emailasinenv@hosting.com', 'ShortName');
        $message->to( $data['adminEmail'] )->subject("Contact Form" );
    }); 
    return true;
}
catch (\Exception $ex) {
    $ex->getMessage();
    return false;            
}

答案 2 :(得分:0)

由于您的代码中已有一个模板,请使用该模板,将消息传递给模板

$subject = 'Email Subject';
Mail::send('emails.notification', ['message' => $message], function ($mail) use ($userEmail, $subject) {
    $mail->from('info@domain.com', 'Domain Name');
    $mail->to($userEmail)->subject($subject);
});