如何使用Laravel在一个按钮上发送两个不同的电子邮件

时间:2017-05-15 17:37:19

标签: php laravel email

我正在尝试在用户提交联系表单的同时发送两封电子邮件。一封电子邮件发送给网站所有者,另一封发送给用户作为自动回复。我一直试图在最近4个小时内完成这项工作并尝试在互联网上使用不同的解决方案,但我完全迷失了。这是我发送电子邮件的代码

  public function contactForm(Request $request)
{
    $parameters = Input::get();
    $email = Input::get('email');
    $inquiryType = Input::get('type_inquiry');
    foreach ([
                 'contactmessage' => 'Message',
                 'email'        => 'Email',
                 'phone'        => 'Phone',
                 'first_name'   => 'Contact Name',
                 'g-recaptcha-response' => 'Captcha',
             ] as $key => $label) {
        if (!isset($parameters[$key]) || empty($parameters[$key])) {
            return response()->json(
                [
                    'success' => false,
                    'error'   => "{$label} cannot be empty",
                ]
            );
        }
    }


   $recipients = 'abc@gmail.com';

    // if page set, try to get recipients from the page settings
    if (Input::get('page_id')) {
        $page = Page::find(Input::get('page_id'));
        if ($page && !empty($page->recipients)) {
            $recipients = explode(',', $page->recipients);
        }
    }

    try {
            $res = Mail::send(
                'emails.contact',
                $parameters,
                function (Message $message) use ($recipients) {
                    $message->subject('Contact message');
                    if (is_array($recipients)) {
                        // email to first address
                        $message->to(array_shift($recipients));
                        // cc others
                        $message->cc($recipients);
                    } else {
                        $message->to($recipients);
                    }
                }
            );

    } catch (\Exception $e) {
        return response()->json(
            [
                'success' => false,
                'error'   => $e->getMessage(),
            ]
        );
    }

 if($inquiryType == 'Rental Inquiry'){
    Mail::send(
        'emails.autoresponse',
        '',
        function (Message $message) use ($email) {
            $message->subject('Thank you for inquiring');
            if (is_array($email) {
                // email to first address
                $message->to(array_shift($email);
                // cc others
                $message->cc($email);
            } else {
                $message->to($email);
            }
        }
    );
  }
    return response()->json(
        [
            'success' => $res,
        ]
    );
}

我试图通过不同的方法做同样的事情,但没有一个是有效的。请帮我。这是我第一次使用laravel发送多封电子邮件。我想我在某个地方犯了一个大而愚蠢的错误。

谢谢。

2 个答案:

答案 0 :(得分:1)

最好的方法是创建一个Laravel Jobs

php artisan queue:table
php artisan migrate

php artisan make:job SendEmail

修改您的.env

QUEUE_DRIVER=database

修改您的app /Jobs/SendEmail.php

namespace App\Jobs;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Mail\Mailer;

class SendEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $subject;
    protected $view;
    protected $data;
    protected $email;

    /**
     * SendEmail constructor.
     * @param $subject
     * @param $data
     * @param $view
     * @param $email
     */
    public function __construct($subject, $data, $view, $email)
    {
        $this->subject = $subject;
        $this->data = $data;
        $this->email = $email;
        $this->view = $view;
    }

    /**
     * Execute the job.
     * @param $mailer
     * @return void
     */
    public function handle(Mailer $mailer)
    {
        $email = $this->email;
        $subject = $this->subject;
        $view = $this->view;
        $mailer->send($view, $this->data,
            function ($message) use ($email, $subject) {
                $message->to($email)
                    ->subject($subject);
            }
        );
    }
}

并在你的控制器中处理

use App\Jobs\SendEmail;

public function contactForm(Request $request) {
    //TODO Configure Subject
    $subjectOwner    = 'Your Email Subject For Owner';
    $subjectUser    = 'Your Email Subject For User';
    //TODO Configure Email
    $ownerEmail = 'ownerEmail@gmail.com';
    $userEmail = 'userEmail@gmail.com';
    //TODO Configure Data Email send to email blade viewer
    $dataEmail = [
        'lang'                  => 'en',
        'user_name'             => 'User Name'
    ];
    //emails.owner mean emails/owner.blade.php
    //emails.admin mean emails/admin.blade.php
    $jobOwner = (new SendEmail($subjectOwner, $dataEmail,  "emails.owner" , $ownerEmail))->onQueue('emails');
    dispatch($jobOwner);
    $jobUser = (new SendEmail($subjectUser, $dataEmail,  "emails.admin" , $userEmail))->onQueue('emails');
    dispatch($jobUser);
}

尝试命令

//IF You using Laravel 5.2
php artisan queue:listen --queue=emails
//IF You using Laravel >5.3
php artisan queue:work

答案 1 :(得分:1)

is_array($email)

附近缺少右括号
$message->subject('Thank you for inquiring');
if (is_array($email)) {

另外,我会使用laravel的验证器来检查所需的输入。另一个建议是使用队列邮件。在单个请求中发送两封邮件可能会导致页面加载时间显着增加。