Home > php教程 > php手册 > PHPMailer 发送邮件实例程序

PHPMailer 发送邮件实例程序

WBOY
Release: 2016-05-25 16:49:17
Original
1340 people have browsed it

首先,先到http://phpmailer.sourceforge.net/这个地址去下载最新的PHPMailer的包(PHPMailer方法必须使用这个包),下载完成后解压到相应的目录,可以看到解压后的文件夹里面的class.phpmailer.php(调用PHPMailer需要包含这个文件),代码如下:

<?php     
require("phpmailer/class.phpmailer.php");     
function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){     
$mail = new PHPMailer();     
$mail->IsSMTP();                  // send via SMTP     
$mail->Host = "smtp.163.com";   // SMTP servers     
$mail->SMTPAuth = true;           // turn on SMTP authentication     
$mail->Username = "xuchao842363331";     // SMTP username  注意:普通邮件认证不需要加 @域名  这里是我的163邮箱 
$mail->Password = "password"; // SMTP password    在这里输入邮箱的密码 
$mail->From = "xuchao842363331@163.com";      // 发件人邮箱     
$mail->FromName =  "管理员";  // 发件人     

$mail->CharSet = "UTF-8";   // 这里指定字符集!    指定UTF-8后邮件的标题和发件人等等不会乱码,如果是GB2312标题会乱码 
$mail->Encoding = "base64";     
$mail->AddAddress($sendto_email,"username");  // 收件人邮箱和姓名     
$mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com");     
//$mail->WordWrap = 50; // set word wrap 换行字数     
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件     
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");     
//$mail->IsHTML(true);  // send as HTML     
// 邮件主题     
$mail->Subject = $subject;     
// 邮件内容     
$mail->Body = "hello!PHPMailer";        
//$mail->AltBody ="text/html";     
if(!$mail->Send())     
{     
	echo "error <p>";     
	echo "error: " . $mail->ErrorInfo;     
	exit;     
}     
else {     
	echo"success!";  
}     
}     
// 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)       
?>
Copy after login

这里要说明一下:当字符集指定为GB2312的时候标题会乱码,这里指定为UTF-8不会出现乱码.

其实,PHPMailer还有很多功能,比如可以添加附件等等,这里就不再演示了,这样在你需要用邮件功能的时候调用这个函数就可以了,代码如下:

<?php
require("mail.php"); 
smtp_mail("842363331@qq.com", "催还", "", "", "username");     
?>
Copy after login

文章链接:

随便收藏,请保留本文地址!

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template