PHP 发送邮件配置 转发自http://www.cnblogs.com/dee0912/p/5448350.html
PHP (sendmail / PHPMailer / ezcMailComposer)发送邮件
一. 使用 PHP 内置的 mail() 函数
1. Windows 下
1
|
环境:WampServer2.5(Windows 10,Apache 2.4.9,MySQL 5.6.17,PHP 5.5.12)
|
① 在 Windows 下使用 PHP 内置的 mail() 函数发送邮件,需要先安装 sendmail(下载地址:http://glob.com.au/sendmail/)
把下载下来的 sendmail.zip 解压到自定义的目录(我这里是 D:\wamp\bin)
② 配置 php.ini 文件(通过 phpinfo 确定 ph.ini 文件真实路径)
邮件服务器以腾讯邮箱为例,php.ini 文件主要配置
SMTP = smtp.qq.com
smtp_port = 25(邮件服务端口),
sendmail_path = "D:\wamp\bin\sendmail\sendmail.exe -t"
③ 配置 sndmail.ini
需要配置:
1
2
3
4
5
6
7
8
9
10
11
|
smtp_server=smtp.qq.com
smtp_port=25
,开启 log 方便排错,生成的log文件在sendmail根目录
error_logfile=error.log
debug_logfile=debug.log
auth_username=472323087@qq.com
auth_password=你的授权码
,force_sender 要和auth_username一致
force_sender=472323087@qq.com
|
说明:
配置项中的 auth_password 不是邮箱的密码, 而是腾讯邮箱的第三方客户端授权码,获取授权码的方式是:
登陆 mail.qq.com(472323087@qq.com),选择“设置” -- “POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务” -- “生成授权码”
点击“生成授权码”,需要发送一条验证信息,验证通过之后得到授权码
④ 测试发送邮件:
1
2
3
4
5
6
7
8
|
<?php
// 使用 PHP 内置的 mail() 函数
$to = '472323087@qq.com' ;
$subject = 'Hello World!' ;
$body = 'Welcome to China!' ;
mail( $to , $subject , $body );
|
收到邮件:
2. Linux 下
1
|
环境:LNMP(CentOS 6.6 ,Nginx 1.8.0,MySQL 5.6.23,PHP 5.6.9)
|
① 安装 sendmail
1
|
# yum install sendmail
|
② 启动 sendmail
1
|
# /etc/rc.d/init.d/sendmail start
|
③ 此时可以直接通过 mail 命令来给指定邮箱发送邮件:
1
|
[root@localhost ~]# echo 'this is a mail test' |mail -s text dee1566@126.com
|
这里先用 126 邮箱举例,腾讯邮箱由于腾讯邮件服务器的限制,不做设置很容造成拒收,后面再说。
打开邮件:
④ 使用 PHP 的 mail() 函数发送邮件
需要修改 php.ini
1
2
|
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t
|
SMTP 不用设置
mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
header( 'Content-type:text/html;charset=utf-8' );
// 使用 PHP 内置的 mail() 函数
$to = 'dee1566@126.com' ;
$subject = 'Hello World!' ;
$body = 'Welcome to China!' ;
if (mail( $to , $subject , $body )) {
echo '发送成功' ;
} else {
echo '发送失败' ;
}
|
收到邮件:
打开邮件:
说明:
1
|
mail( "接受方email" , "邮件主题" , "正文内容" , headers, "from:发送方email" );
|
要修改发件人,可以添加第四个参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
header( 'Content-type:text/html;charset=utf-8' );
// 使用 PHP 内置的 mail() 函数
$to = (isset( $_GET [ 'type' ]) && $_GET [ 'type' ] == 'qq' ) ? '472323087@qq.com' : 'dee1566@126.com' ;
$subject = 'Hello World!' ;
$body = 'Welcome to China!' ;
$headers = array ();
$headers [] = "MIME-Version: 1.0" ;
$headers [] = "Content-type: text/plain; charset=iso-8859-1" ;
$headers [] = "From: dee <472323087@qq.com>" ;
$from = '472323087@qq.com' ;
if (mail( $to , $subject , $body , implode( "\r\n" , $headers ), $from )) {
echo '发送成功' ;
} else {
echo '发送失败' ;
}
|
此时收到的邮件:
打开邮件:
⑤ 如果接收方 email 是腾讯邮箱的话,很容易显示发送成功但是实际上根本就没有发送成功,通过查看日志
1
|
[root@localhost sbin]# tail -f / var /spool/mail/root
|
可能会出现
1
2
|
<<< 550 Mail content denied. http: //service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726
554 5.0.0 Service unavailable
|
550 Mail content denied 出错原因:该邮件内容涉嫌大量群发,并且被多数用户投诉为垃圾邮件
编辑 /etc/mail.rc
1
|
[root@localhost ~]# vim /etc/mail.rc
|
添加:
1
2
|
set from=472323087\@qq.com smtp=smtp.qq.com //邮件来自
set smtp-auth-user=472323087\@qq.com smtp-auth-password=你的授权码 smtp-auth=login //登录qq SMTP服务器的用户名和密码
|
此时直接使用 mail 命令发送邮件:
1
|
[root@localhost ~]# echo 'this is a mail test' |mail -s title 472323087@qq.com
|
但还是不能使用 PHP 的 mail 函数给腾讯邮箱发送邮件,查了很多资料,还是没能解决。
总结:
在 Windows 下使用 sendmail 结合 mail() 函数能很容易地给腾讯邮箱发邮件,在 Linux 下腾讯邮箱几乎一律拒收,连垃圾箱都进不了,其他的比如 126 邮箱就没有问题,可能还是和主机设置有关。
最终代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
header( 'Content-type:text/html;charset=utf-8' );
// 使用 PHP 内置的 mail() 函数
$to = 'dee1566@126.com' ;
$subject = 'Hello World!' ;
$body = 'Welcome to China!' ;
$headers = array ();
$headers [] = "MIME-Version: 1.0" ;
$headers [] = "Content-type: text/plain; charset=iso-8859-1" ;
$headers [] = "From: dee <472323087@qq.com>" ; //决定邮件的发件人显示
$from = '472323087@qq.com' ;
if (mail( $to , $subject , $body , implode( "\r\n" , $headers ), $from )) {
echo '发送成功' ;
} else {
echo '发送失败' ;
}
|
二. 使用 Zeatcomponent 的 ezcMailComposer 类
文档地址:http://ezcomponents.org/docs/tutorials/Mail
下载地址:http://ezcomponents.org/download
下载后解压压缩包
Zetacomponent ezcMailComposer 类可以与 SMTP 服务器直接通信:
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
|
<?php
header( 'Content-type:text/html;charset=utf-8' );
// 使用 Zeccomponent 的 ezcMailComposer 类
// http://ezcomponents.org/docs/tutorials/Mail
require_once 'ezcomponents/Mail/docs/tutorial/tutorial_autoload.php' ;
$message = new ezcMailComposer();
$message ->from = new ezcMailAddress( '47232087' , 'dee' ); //发送邮箱是qq邮箱,例如472323087@qq.com,邮件很容易被拒,sendmail根目录下error.log中错误记录是:Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.html<EOL>
$message ->addTo( new ezcMailAddress( '472323087@qq.com' , 'emperor' ));
$message ->subject = 'php sendmail' ;
$body = 'this is a test mail' ;
$message ->plainText = $body ;
$message ->build();
$host = 'smtp.qq.com' ;
$username = '472323087@qq.com' ;
$password = 'niwogqkejpnzbibh' ;
$port = '25' ;
$smtpOptions = new ezcMailSmtpTransportOptions();
$smtpOptions ->preferredAuthMethod = ezcMailSmtpTransport::AUTH_LOGIN;
$sender = new ezcMailMtaTransport( $host , $username , $password , $port , $smtpOptions );
try {
$sender ->send( $message );
echo '发生成功' ;
} catch (ezcMailTransportException $e ) {
echo $e ->getMessage();
}
|
收到邮件:
打开邮件:
同样在 Linux 下同样会遇到腾讯邮箱直接拒收的问题。
三. 使用 PHPMailer 类
PHPMailer 版本 5.2.13
下载地址:https://github.com/Synchro/PHPMailer
Windows 下调试代码:
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
|
<?php
header( "content-type:text/html;charset=utf-8" );
require 'PHPMailer/class.smtp.php' ;
require 'PHPMailer/class.phpmailer.php' ;
try {
$mail = new PHPMailer(true);
$mail ->IsSMTP();
$mail ->CharSet= 'UTF-8' ; //设置邮件的字符编码,这很重要,不然中文乱码
$mail ->SMTPAuth = true; //开启认证
$mail ->Port = 25;
$mail ->Host = "smtp.qq.cn" ;
$mail ->Username = "472323087@qq.com" ;
$mail ->Password = "你的授权码" ;
$mail ->IsSendmail(); //windows下开启;linux下如果没有sendmail组件就注释掉,否则出现“Could not execute: /usr/sbin/sendmail”的错误提示
$mail ->AddReplyTo( "472323087@qq.com" , "dee" ); //回复地址
$mail ->From = "472323087@qq.com" ;
$mail ->FromName = "472323087@qq.com" ;
$to = "472323087@qq.com" ;
$mail ->AddAddress( $to );
$mail ->Subject = "phpmailer测试标题" ;
$mail ->Body = "<h1>phpmail演示</h1>这是emperor对phpmailer的测试内容" ;
$mail ->AltBody = "To view the message, please use an HTML compatible email viewer!" ; //当邮件不支持html时备用显示,可以省略
$mail ->WordWrap = 80; // 设置每行字符串的 长度
//$mail->AddAttachment("d:/test.jpg"); //可以添加附件
$mail ->IsHTML(true);
$mail ->Send();
echo '邮件已发送' ;
} catch (phpmailerException $e ) {
echo "邮件发送失败:" . $e ->errorMessage();
}
|
Linux 下修改 php.ini,注释
sendmail_path = /usr/sbin/sendmail -t -i
把程序中的
1
|
$mail ->IsSendmail();
|
也注释,可以完成包括对腾讯邮箱的邮件任务。
注意,邮件服务器尽量不要选腾讯邮箱。
PHP 发送邮件配置 转发自http://www.cnblogs.com/dee0912/p/5448350.html相关推荐
- Jenkins+Gradle+Gitlab+蒲公英 +打包成功后发送邮件配置
一.配置全局属性 Environment variables ANDROID_HOME SDK路径 /Users/lejumobile/Library/Android/sdk GRADLE_USER_ ...
- Java mail使用QQ邮箱发送邮件配置方案
Java mail使用QQ邮箱发送邮件配置方案 java mail配置了QQ邮箱作为服务器发送邮件,结果导致出现503异常: Authentication failed; nested excepti ...
- java Mail使用SSl,端口465加密发送邮件配置
转载于 https://www.cnblogs.com/hoojjack/p/8025284.html 一.一般配置 发送邮件需要用到 mail包 maven 依赖如下: 1 <!-- htt ...
- github上的优秀项目和开发环境配置【转http://www.cnblogs.com/2018/archive/2012/11/09/2763119.html】...
github上的优秀项目和开发环境配置 国外的几个公司开放的资源 https://github.com/google https://github.com/facebook https://git ...
- dyanmics crm workflow 工作流发送邮件配置
接到业务要求需要发送邮件,最近发送邮件出来一个莫名其妙的问题,提示connot create the given type without the required parameters 没有必须的参 ...
- Confluence 6 为发送邮件配置服务器
配置你的 Confluence 服务器发送电子邮件消息能够允许你的 Confluence 用户: 接受邮件通知和每天更新报表. 通过电子邮件发送一个页面. 你可以通过配置 'From' 字段中的内容来 ...
- django腾讯企业邮箱发送邮件配置
首先域名解析设置好到腾讯的服务器 django settings文件中设置好.如下 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBacke ...
- Java + 腾讯企业邮箱 + javamail + SSL 发送邮件(转载:http://www.cnblogs.com/LUA123/p/5575134.html)
说实话腾讯的企业邮箱真心不错! 腾讯企业邮箱官网:http://exmail.qq.com/login/ 新用户注册:http://exmail.qq.com/onlinesell/intro 点击开 ...
- Ubuntu 16.04使用阿里云邮箱命令行发送邮件配置(完整亲测版)
1.安装mailutils和heirloom-mailx,如果非root用户请使用sudo权限 apt-get install mailutils apt-get install heirloom-m ...
最新文章
- swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B
- 得到java异常printStackTrace的详细信息
- java itext 设计器_使用Java组件itext 生成pdf的介绍
- Xamarin提示Build-tools版本过老
- Spring Cloud Alibaba - 17 Nacos Config 配置中心 应用篇
- PHP 6:PHP 基本数据类型
- ENVI计算公式(一)
- caffe报错:Check failed: width <= datum_width (28 vs. 17)
- 一个小案例精通lamda表达式与函数式接口
- es6 super关键字
- Windows 7 Ultimate + Ubuntu 12.04 LTS双系统完美走起
- 形容时间过得快的句子,一些表示时间过得快的句子
- 高性能计算机储存部件硬盘,高性能计算机的磁盘系统结构.pdf
- 如何进行cad地理配准_如何对扫描图片进行ArcGIS配准?
- java技术简介英文_JAVA技术人员英文简历模板
- IntelliJ IDEA 2017 破解
- SSM项目实战之十一:用户信息的添加
- 基于深度学习实现手写汉字识别
- 新装Kubuntu 18.04后的一些操作
- 布隆过滤器在HBase中的应用
热门文章
- yota3墨水屏调节对比度_YOTA3墨水屏“直通”技术取代旧“投射”功能,体验全面升级-非常在线...
- 基于同态加密体制的安全多方计算
- 如何快速上手Mac电脑?mac新手教程 Mac电脑新手必看
- 十二时辰与时间对照表,十二经络时辰表
- 社交登陆,分布式session,单点登陆,jwt
- ADCIRC模式与Python融合技术应用
- OPNET网络仿真分析-前言
- 最新云开秒赞系统公益版网站源码
- new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
- 程序史简史:从巴贝奇到爱达在到图灵