Home php教程 php手册 [转]PHP发送邮件

[转]PHP发送邮件

Jun 14, 2016 am 12:02 AM
php code send email Open source programming programming language software development

这是用开源项目PHPMailer实现邮件发送 ,先下载文件,我这里下载得是5.1得版本,然后把三个class.***.php文件放到项目文件下,我首先用得是gmail,但是失败了,问题是认证失败,后来用了QQ邮箱,发送成功。
require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录
$mail = new PHPMailer(); //建立邮件发送类
$address ="youbinliu@126.com";
$mail->IsSMTP(); // 使用SMTP方式发送
$mail->Host = "smtp.qq.com"; // 您的企业邮局域名
$mail->SMTPAuth = true; // 启用SMTP验证功能
$mail->Username = "843831601@qq.com"; // 邮局用户名(请填写完整的email地址)
$mail->Password = "***********"; // 邮局密码
$mail->Port=25;
$mail->From = "843831601@qq.com"; //邮件发送者email地址
$mail->FromName = "liuyoubin";
$mail->AddAddress("$address", "a");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");

//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

$mail->Subject = "PHPMailer测试邮件"; //邮件标题
$mail->Body = "Hello,这是测试邮件"; //邮件内容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略

if(!$mail->Send())
{
echo "邮件发送失败.

";
echo "错误原因: " . $mail->ErrorInfo;
exit;
}

echo "邮件发送成功";
?>

附件:
phpmailer 中文使用说明(简易版)
A开头:
$AltBody--属性
出自:PHPMailer::$AltBody
文件:class.phpmailer.php
说明:该属性的设置是在邮件正文不支持HTML的备用显示
AddAddress--方法
出自:PHPMailer::AddAddress(),文件:class.phpmailer.php
说明:增加收件人。参数1为收件人邮箱,参数2为收件人称呼。例 AddAddress("eb163@eb163.com","eb163"),但参数2可选,AddAddress(eb163@eb163.com)也是可以的。
函数原型:public function AddAddress($address, $name = '') {}
AddAttachment--方法
出自:PHPMailer::AddAttachment()
文件:class.phpmailer.php。
说明:增加附件。
参数:路径,名称,编码,类型。其中,路径为必选,其他为可选
函数原型:
AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){}
AddBCC--方法
出自:PHPMailer::AddBCC()
文件:class.phpmailer.php
说明:增加一个密送。抄送和密送的区别请看[SMTP发件中的密送和抄送的区别] 。
参数1为地址,参数2为名称。注意此方法只支持在win32下使用SMTP,不支持mail函数
函数原型:public function AddBCC($address, $name = ''){}
AddCC --方法
出自:PHPMailer::AddCC()
文件:class.phpmailer.php
说明:增加一个抄送。抄送和密送的区别请看[SMTP发件中的密送和抄送的区别] 。
参数1为地址,参数2为名称注意此方法只支持在win32下使用SMTP,不支持mail函数
函数原型:public function AddCC($address, $name = '') {}
AddCustomHeader--方法
出自:PHPMailer::AddCustomHeader()
文件:class.phpmailer.php
说明:增加一个自定义的E-mail头部。
参数为头部信息
函数原型:public function AddCustomHeader($custom_header){}
AddEmbeddedImage --方法
出自:PHPMailer::AddEmbeddedImage()
文件:class.phpmailer.php
说明:增加一个嵌入式图片
参数:路径,返回句柄[,名称,编码,类型]
函数原型:public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {}
提示:AddEmbeddedImage(PICTURE_PATH. "index_01.jpg ", "img_01 ", "index_01.jpg ");
在html中引用
AddReplyTo--方法
出自:PHPMailer:: AddReplyTo()
文件:class.phpmailer.php
说明:增加回复标签,如"Reply-to"
参数1地址,参数2名称
函数原型:public function AddReplyTo($address, $name = '') {}
AddStringAttachment-方法
出自:PHPMailer:: AddStringAttachment()
文件:class.phpmailer.php
说明:增加一个字符串或二进制附件(Adds a string or binary attachment (non-filesystem) to the list.?)
参数:字符串,文件名[,编码,类型]
函数原型:public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {}
Authenticate--方法
出自:SMTP::Authenticate()
文件:class.smtp.php
说明:开始SMTP认证,必须在Hello()之后调用,如果认证成功,返回true,
参数1用户名,参数2密码
函数原型:public function Authenticate($username, $password) {}
B开头
$Body--属性
出自:PHPMailer::$Body
文件: class.phpmailer.php
说明:邮件内容,HTML或Text格式
C开头
$CharSet--属性
出自:PHPMailer::$CharSet
文件:class.phpmailer.php
说明:邮件编码,默认为iso-8859-1
$ConfirmReadingTo--属性
出自:PHPMailer::$ConfirmReadingTo 文件class.phpmailer.php
说明:回执?
$ContentType--属性
出自:PHPMailer::$ContentType
文件: class.phpmailer.php
说明:文档的类型,默认为"text/plain"
$CRLF--属性
出自:PHPMailer::$ContentType
文件:class.phpmailer.php
说明:SMTP回复结束的分隔符(SMTP reply line ending?)
class.phpmailer.php--对象
出自:class.phpmailer.php
文件: class.phpmailer.php
说明:phpmailer对象
class.smtp.php--对象
出自:class.smtp.php 文件: class.smtp.php
说明:SMTP功能的对象
ClearAddresses--方法
出自:PHPMailer::ClearAddresses()
文件: class.phpmailer.php
说明:清除收件人,为下一次发件做准备。返回类型是void
ClearAllRecipients--方法
出自:PHPMailer::ClearAllRecipients()
文件: class.phpmailer.php
说明:清除所有收件人,包括CC(抄送)和BCC(密送)
ClearAttachments--方法
出自:PHPMailer::ClearAttachments()
文件: class.phpmailer.php
说明:清楚附件
ClearBCCs--方法
出自:PHPMailer::ClearBCCs() 文件 class.phpmailer.php
说明:清楚BCC (密送)
ClearCustomHeaders--方法
出自:PHPMailer::ClearCustomHeaders()
文件: class.phpmailer.php
说明:清楚自定义头部
ClearReplyTos--方法
出自:PHPMailer::ClearReplyTos()
文件: class.phpmailer.php
说明:清楚回复人
Close--方法
出自:SMTP::Close()
文件: class.smtp.php
说明:关闭一个SMTP连接
Connect--方法
出自:SMTP::Connect()
文件: class.smtp.php
说明:建立一个SMTP连接Mailer.html
$ContentType--属性
出自:PHPMailer::$ContentType
文件: class.phpmailer.php
说明:文档的类型,默认为"text/plain"
D开头
$do_debug--属性
出自:SMTP::$do_debug
文件:class.smtp.php
说明:SMTP调试输出
Data-方法
出自:SMTP::Data()
文件:class.smtp.php
说明:向服务器发送一个数据命令和消息信息(sendsthemsg_datatotheserver)
E开头
$Encoding--属性
出自:PHPMailer::$Encoding
文件:class.phpmailer.php
说明:设置邮件的编码方式,可选:"8bit","7bit","binary","base64",和"quoted-printable".
$ErrorInfo--属性
出自:PHPMailer::$ErrorInfo
文件:class.phpmailer.php
说明:返回邮件SMTP中的最后一个错误信息
Expand--方法
出自:SMTP::Expand()
文件:class.smtp.php
说明:返回邮件列表中所有用户。成功则返回数组,否则返回 false(Expandtakesthenameandaskstheservertolistallthepeoplewhoaremembersofthe_list_.Expandwillreturnbackandarrayoftheresultorfalseifanerroroccurs.)
F开头:
$From--属性
出自:PHPMailer::$From文件class.phpmailer.php
说明:发件人E-mail地址
$FromName--属性
出自:PHPMailer::$FromName
文件:class.phpmailer.php
说明:发件人称呼
H开头:
$Helo--属性
出自:PHPMailer::$Helo
文件:class.phpmailer.php
说明:设置SMTPHelo,默认是$Hostname(SetstheSMTPHELOofthemessage(Defaultis$Hostname).)
$Host--属性
出自:PHPMailer::$Host
文件:class.phpmailer.php
说明:设置SMTP服务器,格式为:主机名[端口号],如smtp1.example.com:25和smtp2.example.com都是合法的
$Hostname--属性
出自:PHPMailer::$Hostname
文件:class.phpmailer.php
说明:设置在Message-Id和andReceivedheaders中的hostname并同时被$Helo使用。如果为空,默认为SERVER_NAME或'localhost.localdomain"
Hello--方法
出自:SMTP::Hello()
文件:class.smtp.php
说明:向SMTP服务器发送HELO命令
Help--方法
出自:SMTP::Help()
文件:class.smtp.php
说明:如果有关键词,得到关键词的帮助信息
I开头:
IsError--方法
出自:PHPMailer::IsError()
文件:class.phpmailer.php
说明:返回是否有错误发生
IsHTML--方法
出自:PHPMailer::IsHTML()
文件:class.phpmailer.php
说明:设置信件是否是HTML格式
IsMail--方法
出自:PHPMailer::IsMail()
文件:class.phpmailer.php
说明:设置是否使用php的mail函数发件
IsQmail--方法
出自:PHPMailer::IsQmail()
文件:class.phpmailer.php
说明:设置是否使用qmailMTA来发件
IsSendmail--方法
出自:PHPMailer::IsSendmail()
文件:class.phpmailer.php
说明:是否使用$Sendmail程序来发件
IsSMTP--方法
出自:PHPMailer::IsSMTP()
文件:class.phpmailer.php
说明:是否使用SMTP来发件
M开头:
$Mailer--属性
出自:PHPMailer::$Mailer
文件:class.phpmailer.php
说明:发件方式,("mail","sendmail",or"smtp").中的一个
Mail--方法
出自:SMTP::Mail()
文件:class.smtp.php
说明:从$from中一个邮件地址开始处理,返回true或false。如果是true,则开始发件
N开头:
Noop--方法
出自:SMTP::Noop()
文件:class.smtp.php
说明:向SMTP服务器发送一个NOOP命令
P开头:
$Password--属性
出自:PHPMailer::$Password
文件:class.phpmailer.php
说明:设置SMTP的密码
$PluginDir--属性
出自:PHPMailer::$PluginDir
文件:class.phpmailer.php
说明:设置phpmailer的插件目录,仅在smtpclass不在phpmailer目录下有效
$Port--属性
出自:PHPMailer::$Port
文件:class.phpmailer.php
说明:设置SMTP的端口号
$Priority--属性
出自:PHPMailer::$Priority
文件:class.phpmailer.php
说明:设置邮件投递优先等级。1=紧急,3=普通,5=不急
PHPMailer--对象
出自:PHPMailer
文件:class.phpmailer.php
说明:PHPMailer-PHPemailtransportclass
Q开头
Quit--方法
出自:SMTP::Quit()
文件:class.smtp.php
说明:向服务器发送Quit命令,如果没有错误发生。那么关闭sock,不然$close_on_error为true
R开头
Recipient--方法
出自:SMTP::Recipient()
文件:class.smtp.php
说明:使用To向SMTP发送RCPT命令,参数为:$to
Reset--方法
出自:SMTP::Reset()
文件:class.smtp.php
说明:发送RSET命令从而取消处理中传输。成功则返回true,否则为false
S开头:
$Sender--属性
出自:PHPMailer::$Sender
文件:class.phpmailer.php
说明:SetstheSenderemail(Return-Path)ofthemessage.Ifnotempty,willbesentvia-ftosendmailoras'MAILFROM'insmtpmode.
$Sendmail--属性
出自:PHPMailer::$Sendmail
文件:class.phpmailer.php
说明:设置发件程序的目录
$SMTPAuth--属性
出自:PHPMailer::$SMTPAuth
文件:class.phpmailer.php
说明:设置SMTP是否需要认证,使用Username和Password变量
$SMTPDebug--属性
出自:PHPMailer::$SMTPDebug
文件:class.phpmailer.php
说明:设置SMTP是否调试输出?
$SMTPKeepAlive--属性
出自:PHPMailer::$SMTPKeepAlive
文件:class.phpmailer.php
说明:在每次发件后不关闭连接。如果为true,则,必须使用SmtpClose()来关闭连接
$SMTP_PORT--属性
出自:SMTP::$SMTP_PORT
文件:class.smtp.php
说明:设置SMTP端口
$Subject--属性
出自:PHPMailer::$Subject
文件:class.phpmailer.php
说明:设置信件的主题
Send--方法
出自:SMTP::Send()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
Send--方法
出自:PHPMailer::Send()
文件:class.phpmailer.php
说明:创建邮件并制定发件程序。如果发件不成功,则返回false,请使用ErrorInfo来查看错误信息
SendAndMail--方法
出自:SMTP::SendAndMail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
SendOrMail--方法
出自:SMTP::SendOrMail()
文件:class.smtp.php
说明:从指定的邮件地址开始一个邮件传输
SetLanguage--方法
出自:PHPMailer::SetLanguage()
文件:class.phpmailer.php
说明:设置phpmailer错误信息的语言类型,如果无法加载语言文件,则返回false,默认为english
SMTP--方法
出自:SMTP::SMTP()
文件:class.smtp.php
说明:初始化一个对象以便数据处于一个已知的状态
SMTP--对象
出自:SMTP
文件:class.smtp.php
说明:SMTP对象
SmtpClose--方法
出自:PHPMailer::SmtpClose()
文件:class.phpmailer.php
说明:如果有活动的SMTP则关闭它。
T开头
$Timeout--属性
出自:PHPMailer::$Timeout
文件:class.phpmailer.php
说明:设置SMTP服务器的超时(单位:秒)。注意:在win32下,该属性无效
Turn--方法
出自:SMTP::Turn()
文件:class.smtp.php
说明:这是一个可选的SMTP参数,目前phpmailer并不支持他,可能未来支持
U开头
$Username--属性
出自:PHPMailer::$Username
文件:class.phpmailer.php
说明:设置SMTP用户名
V开头
$Version--属性
出自:PHPMailer::$Version
文件:class.phpmailer.php
说明:返回Phpmailer的版本
Verify--方法
出自:SMTP::Verify()
文件:class.smtp.php
说明:通过服务器检查用户名是否经过验证
W开头:
$WordWrap--属性
出自:PHPMailer::$WordWrap
文件:class.phpmailer.php
说明:设置每行最大字符数,超过改数后自动换行

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles