ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件
来源:互联网 发布:淘宝宝贝尺寸规格设置 编辑:程序博客网 时间:2024/06/14 11:27
转自:http://www.cnblogs.com/dee0912/p/5451564.html#3443851
ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件
SwiftMailer 下载地址:https://github.com/swiftmailer/swiftmailer
版本:swiftmailer-5.x
把压缩包解压到 /ThinkPHP/Library/Vendor 中。
配置文件 config.php
1
2
3
4
5
6
7
8
9
10
11
<?php
return
array
(
//'配置项'=>'配置值'
// 邮件配置
'SMTP'
=>
'smtp.XXX.cn'
,
'MAIL_PORT'
=> 25,
'MAIL_USER'
=>
'XXX@XXX.com'
,
//邮箱用户名
'MAIL_PWD'
=>
'XXX'
,
//发送邮箱密码或者授权码
'MAIL_FROM'
=>
'XXX@XXX.com'
,
'MAIL_FROM_NAME'
=>
'dee'
,
);
/Application/Home/Common/Swiftmail.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace
Home\Common;
class
Swiftmail {
// @param $host 邮件服务器地址
// @param $port 端口号
// @param $encryption_type 加密方式(例如:使用腾讯qq邮箱时此处填ssl,不加密不填写此项)
// @param $user 用户名
// @param $pwd 密码或授权码
// @param $subject 邮件主题
// @param $body 邮件内容
// @param $from 邮件来自邮箱号
// @param $from_name 邮件来自名称
// @param $to 收件人邮箱
public
static
function
sendMail(
$to
,
$subject
,
$body
,
$encryption_type
= null) {
$host
= C(
'SMTP'
);
$port
= C(
'MAIL_PORT'
);
$user
= C(
'MAIL_USER'
);
$pwd
= C(
'MAIL_PWD'
);
$from
= C(
'MAIL_FROM'
);
$from_name
= C(
'MAIL_FROM_NAME'
);
Vendor(
'swiftmailer.lib.swift_required'
);
$transport
=\Swift_SmtpTransport::newInstance(
$host
,
$port
,
$encryption_type
)
->setUsername(
$user
)
->setPassword(
$pwd
);
$mailer
=\Swift_Mailer::newInstance(
$transport
);
$message
=\Swift_Message::newInstance()
->setSubject(
$subject
)
->setFrom(
array
(
$from
=>
$from_name
))
->setTo(
$to
)
->setContentType(
"text/html"
)
->setBody(
$body
);
$mailer
->protocol=
'smtp'
;
$mailer
->send(
$message
);
}
}
控制器和方法(按需求确定位置)/Application/Home/Controller/IndexController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace
Home\Controller;
use
Think\Controller;
use
Home\Common\Swiftmail;
class
IndexController
extends
Controller {
public
function
mail_send() {
$to
=
'472323087@qq.com'
;
$subject
=
'SwiftMail测试标题'
;
$body
=
'<h1>SwiftMail演示</h1>这是dee对SwiftMail的测试内容'
;
try
{
Swiftmail::sendMail(
$to
,
$subject
,
$body
);
echo
'success'
;
}
catch
(Swift_RfcComplianceException
$e
) {
echo
$e
->getMessage();
}
}
}
运行后显示 success
收取邮件:
打开邮件:
参考:发送邮件Swift Mailer代替PHPmail
阅读全文
0 0
- ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件
- Nodejs使用mailer发送邮件
- php使用Mailer实现发送邮件
- Nodejs使用mailer发送邮件(未测试)
- PHP mailer邮件发送
- 使用php mailer类调用远程SMTP服务器发送邮件
- Rails使用Action Mailer发送邮件的问题
- Thinkphp使用PHPMailer发送邮件
- PHP Swift Mailer库使用
- php发送邮件 ② Mailer类
- php mailer php发送邮件 发送邮件函数
- freebsd,Svn使用mailer.py不能发送邮件,提示小于1.5.0版本,解决方式
- thinkphp 中 使用PHPMailer发送邮件
- 在thinkphp中使用phpmailer发送邮件
- Thinkphp框架使用PHPMail实现发送邮件
- thinkphp中怎么使用phpmailer发送邮件
- ThinkPHP使用PHPMailer发送Email邮件汇总
- PHP Mailer邮件发送失败发送失败! Mailer Error: SMTP connect() failed.
- 1,单例模式
- Java常用jar包下载地址
- 实验二 Linux用户管理
- jQuery两个选择框的边框变化小练习
- 关于怎么安装markcode
- ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件
- David Silver强化学习课程笔记(四)
- NOIP 冲刺 模板:树的直径
- 基于 Anaconda3 5.0.0 JupyterLab 0.27.0 使用 ftplib 演示 IPv4 和 IPv6 上传文件和下载文件
- 1011 最大公约数GCD——51Nod(__gcd() )
- 数据结构笔记之树与二叉树
- 机器学习(周志华)习题3.3
- RTSP协议分析
- java中HashMap排序中实现Comparator接口