如何在Laravel 5.8中发送电子邮件

时间:2019-06-06 11:24:02

标签: php laravel laravel-5

我是Laravel的初学者。我在项目中使用Laravel 5.8。 我想通过联系表格发送电子邮件。

我有此代码:

public function sendContactForm($request)
    {
        $this->validate($request, [
        'name' => 'required|string',
        'topic' => 'required|string',
        'email' => 'required|email',
        'message' => 'required|string',
        'captcha' => 'required|captcha',
        'acceptReg' => 'required|integer',
    ]);
    $adminEmail = $this->frontendRepository->getSystemAdminEmail();

    $title = $request->input('topic');
    $txt = nl2br($request->input('message'));
    $userName = $request->input('name');
    $email = $request->input('email');
    $ip = $request->getClientIp();
    $dateTime = date('Y-m-d H:i:s');

    $message = "Masz wiadomość ze strony". env('APP_NAME')."<br/>
    <b>Dane wiadomości:</b> $dateTime [$ip]<br/>
    <b>Tytuł wiadomości:</b> $title<br/>
    <b>Imię:</b> $userName<br/>
    <b>Adres email:</b> $email<br/>
    <b>Wiadomość:</b> $txt<br/>";


    Mail::send('emails.welcome', $message, function ($message) {
        //$message->from('us@example.com', 'Laravel');
        $message->to($adminEmail, $name = null);
        $message->from($email, $name = null);
        $message->sender($email, $name = null);
        $message->replyTo($adminEmail, $name = null);
        $message->subject($title);
        //$message->attach($pathToFile, array $options = []);
        //$message->attachData($data, $name, array $options = []);
        $message->getSwiftMessage();
    });
    }

$ adminEmail-这是我的电子邮件

如何通过电子邮件发送此消息?

我有错误:传递给Illuminate \ Mail \ Mailer :: send()的参数2必须为数组类型,给定字符串,在/ var / www / vanessa / vendor / laravel / framework / src / Illuminate /中调用237行上的Support / Facades / Facade.php

我该如何续传?

3 个答案:

答案 0 :(得分:1)

  1. 首先,您必须在app / Mail中创建一个邮件类
  2. 创建视图
  3. 在sendcontactform控制器上,调用邮件类
  4. 别忘了也调用邮件外观:public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors(b => b.WithOrigins("http://localhost:8033", "http://localhost:8034","*").AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); // app.UseCors("corspolicy"); //app.UseSignalR(routes => //{ // routes.MapHub<RealTimeHub>("/api/chat"); //}); app.UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); }); }

示例:

use Illuminate\Support\Facades\Mail;

希望这会有所帮助

答案 1 :(得分:0)

要发送电子邮件,您可能需要添加如下所述的代码片段:

Mail::send('email',
   array(
       'name' => $request->get('name'),
       'email' => $request->get('email'),
       'user_message' => $request->get('message')
   ), function($message)
   {
       $message->from('abc@gmail.com');
       $message->to('test@cloudways.com', 'Admin')->subject('Cloudways 
Feedback');
   });
    }

有关如何发送邮件的详细信息,请参阅此link

答案 2 :(得分:0)

在laravel 5.8中发送电子邮件

Mail::send('blade.php', $email_data, function($message) use ($officer) {
    $message->from('from@gmail.com','web title');
    $message->subject("Appointment Scheduled");
    $message->to($email_data['email']);
});