laravel邮件发送

.env 配置

	MAIL_DRIVER=smtp    //使用方式
	MAIL_HOST=smtp.163.com  //仿照配置即可
	MAIL_PORT=25  //端口
	MAIL_USERNAME=???  //你的邮箱,发送邮件的邮箱
	MAIL_PASSWORD=???  //SMTP邮箱的授权码
	MAIL_ENCRYPTION=null  //默认即可

config/mail.php

	<?php

return [

/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
|            "sparkpost", "log", "array"
|
*/

'driver' => env('MAIL_DRIVER', 'smtp'),

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/

'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/

'port' => env('MAIL_PORT', 587),

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '发送邮件的邮箱'),
    'name' => env('MAIL_FROM_NAME', '发件人名称'),
],

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => env('MAIL_ENCRYPTION', 'tls'),

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/

'markdown' => [
    'theme' => 'default',

    'paths' => [
        resource_path('views/vendor/mail'),
    ],
],

];

代码
M3Email.php —定义的一个邮件邮箱,可以自定义

namespace App\Models;

class M3Email {

	//其中发件人、收件人、抄送 当只有一个人时使用字符串,多个人时使用数组
	public $from; //发件人邮箱
	public $to; //收件人邮箱
	public $cc; //抄送
	public $attach; //附件
	public $subject; //主题
	public $content; //内容
}

调用发送邮件
use Mail;

Mail::send('email_register', ['m3_email' => $m3_email], function ($m) use ($m3_email) {
          //$m->from('hello@app.com', 'Your Application'); //已配置,不需要填写

          $m->to($m3_email->to, '尊敬的用户')
              ->cc($m3_email->cc)
              ->subject($m3_email->subject);
      });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Laravel 中发送邮件非常简单,可以使用内置的邮件服务提供者 Mail,以及支持多种邮件驱动程序(如SMTP、Sendmail、Amazon SES等)。 下面是一个简单的示例,展示如何在 Laravel 中发送电子邮件: ```php use Illuminate\Support\Facades\Mail; use App\Mail\DemoEmail; class DemoController extends Controller { public function sendEmail() { $details = [ 'title' => 'Demo Email', 'body' => 'This is a demo email from Laravel.' ]; Mail::to('example@example.com')->send(new DemoEmail($details)); return "Email sent"; } } ``` 在上面的示例中,我们使用 `Mail` 门面调用 `to()` 方法指定收件人,然后使用 `send()` 方法发送邮件。在 `send()` 方法中,我们传递了一个 `DemoEmail` 类实例,该类是我们定义的一个电子邮件类,它负责构建邮件内容。 以下是一个示例 `DemoEmail` 类: ```php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class DemoEmail extends Mailable { use Queueable, SerializesModels; public $details; public function __construct($details) { $this->details = $details; } public function build() { return $this->subject('Demo Email') ->view('emails.demo'); } } ``` 在上面的示例中,我们定义了一个 `$details` 属性,它持有电子邮件内容的详细信息。然后,我们定义了一个构造函数,它接受 `$details` 并将其赋值给 `$this->details`。最后,我们定义了一个 `build()` 方法,它返回一个邮件视图,并指定了邮件主题。 在 Laravel 中,邮件视图通常存储在 `resources/views/emails` 目录中。例如,在上面的示例中,我们可以创建一个名为 `demo.blade.php` 的视图文件,其中包含电子邮件的内容。 此外,我们还可以通过链式调用方法来添加其他邮件内容,例如添加附件、Carbon副本等。 以上是一个简单的 Laravel 发送邮件的示例。您可以根据自己的需求进行更改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值