您好,登录后才能下订单哦!
# Laravel5.4怎么使用163邮箱发送邮件
## 前言
在Web开发中,邮件发送功能是常见的需求之一。Laravel作为一款流行的PHP框架,提供了简洁优雅的邮件发送功能。本文将详细介绍如何在Laravel5.4中使用163邮箱发送邮件,包含配置、代码实现以及常见问题解决。
## 一、环境准备
### 1.1 确保Laravel版本
首先确认你的Laravel版本是5.4:
```bash
php artisan --version
# 应输出:Laravel Framework 5.4.*
Laravel邮件功能需要SwiftMailer
库,通常已包含在框架中。如果缺失可通过Composer安装:
composer require swiftmailer/swiftmailer
ML_DRIVER=smtp
ML_HOST=smtp.163.com
ML_PORT=465
ML_USERNAME=yourname@163.com
ML_PASSWORD=your_authorization_code
ML_ENCRYPTION=ssl
ML_FROM_ADDRESS=yourname@163.com
ML_FROM_NAME=YourAppName
参数 | 值 | 说明 |
---|---|---|
ML_DRIVER | smtp | 使用SMTP协议 |
ML_HOST | smtp.163.com | 163SMTP服务器 |
ML_PORT | 465⁄994 | SSL加密端口 |
ML_USERNAME | 完整邮箱地址 | 如user@163.com |
ML_PASSWORD | 授权码 | 非邮箱密码 |
ML_ENCRYPTION | ssl | 加密方式 |
创建路由和控制器:
// routes/web.php
Route::get('/sendmail', 'MailController@send');
// app/Http/Controllers/MailController.php
use Mail;
use App\Mail\TestMail;
public function send()
{
$email = 'recipient@example.com';
Mail::to($email)->send(new TestMail());
return '邮件已发送';
}
php artisan make:mail TestMail
编辑生成的app/Mail/TestMail.php
:
public function build()
{
return $this->view('emails.test')
->subject('测试邮件主题');
}
resources/views/emails/test.blade.php
:
<!DOCTYPE html>
<html>
<head>
<title>测试邮件</title>
</head>
<body>
<h1>你好,{{ $user }}!</h1>
<p>这是一封来自Laravel的测试邮件。</p>
</body>
</html>
public function build()
{
return $this->view('emails.test')
->attach(public_path('files/document.pdf'), [
'as' => 'document.pdf',
'mime' => 'application/pdf',
]);
}
修改发送方式:
Mail::to($email)->queue(new TestMail());
需要先配置队列驱动,推荐使用Redis或数据库队列。
Swift_TransportException: Expected response code 250 but got code "535"
解决方案: 1. 确认使用的是授权码而非邮箱密码 2. 检查邮箱地址是否完整(包含@163.com) 3. 重新生成授权码尝试
Connection could not be established with host smtp.163.com
解决方案: 1. 检查服务器防火墙是否放行465端口 2. 尝试使用端口25(非SSL) 3. 更换网络环境测试
在邮件类中设置编码:
public function build()
{
return $this->view('emails.test')
->subject('测试邮件')
->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Content-Type', 'text/html; charset=utf-8');
});
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\OrderShipped;
class MailController extends Controller
{
public function send()
{
$user = [
'name' => '张三',
'email' => 'customer@example.com'
];
try {
Mail::to($user['email'])
->cc('admin@example.com')
->bcc('manager@example.com')
->send(new OrderShipped($user));
return response()->json(['status' => 'success']);
} catch (\Exception $e) {
return response()->json([
'status' => 'error',
'message' => $e->getMessage()
], 500);
}
}
}
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->view('emails.orders.shipped')
->subject('您的订单已发货')
->attach(storage_path('app/public/invoice.pdf'), [
'as' => 'invoice.pdf',
'mime' => 'application/pdf',
]);
}
}
通过本文的详细介绍,你应该已经掌握了在Laravel5.4中使用163邮箱发送邮件的方法。从基础配置到高级功能实现,再到问题排查,这些知识将帮助你构建可靠的邮件发送功能。实际开发中,记得根据业务需求进行适当调整,并始终关注邮件发送的性能和安全性。
如果你遇到本文未覆盖的问题,建议查阅以下资源: - Laravel5.4邮件文档 - 163邮箱帮助中心 - SwiftMailer官方文档 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。