欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

有关phpmailer的详细介绍及使用方法

程序员文章站 2022-09-07 15:00:22
第一,需要下载phpmailer文件包phpmailer. http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket...

第一,需要下载phpmailer文件包phpmailer. http://phpmailer.sourceforge.net/
第二,确认你的服务器系统已经支持socket ,通过phpinfo();查看是否支持sockets(socket 是属于php扩展部分),如果显现为“enabled”,那就是支持了。
第三,把文件解压到你的web服务器目录下,调用类就可以了.
首先包含class.phpmailer.php,然后创建对象,设置参数,调用成员函数。

例1,做成函数方便调用

复制代码 代码如下:

<?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 = "200.162.244.66";   // smtp servers   
        $mail->smtpauth = true;           // turn on smtp authentication   
        $mail->username = "yourmail";     // smtp username  注意:普通邮件认证不需要加 @域名   
        $mail->password = "mailpassword"; // smtp password   
        $mail->from = "yourmail@yourdomain.com";      // 发件人邮箱   
        $mail->fromname =  "管理员";  // 发件人   

        $mail->charset = "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 = "  
    <html><head>  
    <meta http-equiv="content-language" content="zh-cn">  
    <meta http-equiv="content-type" content="text/html; charset=gb2312">  
    </head>  
    <body>  
    i love php。  
    </body>  
    </html>  
    ";                                                                         
        $mail->altbody ="text/html";   
        if(!$mail->send())   
        {   
            echo "邮件发送有误 <p>";   
            echo "邮件错误信息: " . $mail->errorinfo;   
            exit;   
        }   
        else {   
            echo "$user_name 邮件发送成功!<br />";   
        }   
    }   
    // 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名)   
    smtp_mail("yourmail@yourdomain.com", "欢迎使用phpmailer!", "null", "yourdomain.com", "username");   
    ?>

注意:

1. 邮件的字符集设置, $mail->charset = "gb2312"; // 这里指定字符集!在这里我只指定为gb2312因为这样outlook能正常显示邮件主题,我尝试过设为utf-8但在outlook下显示乱码。
2. 如果是发送html格式的邮件,那么记得也指定
3. 如果你想用它来群发邮件的话,记得修改包含文件函数,如:
require("phpmailer/class.phpmailer.php");
改为
require_once("phpmailer/class.phpmailer.php");
否则的话会产生类的重定义。

个人认为要使用phpmailer,首先,需要有一个邮件服务器,php的 mail函数没有指定,应该是使用的php设置的smtp。

而在这里需要具体指定,同时需要指定邮件服务器的管理者和密码。

phpmailer 也是一个功能强大的邮件类

phpmailer的主要功能特点:

支持邮件 s/mime加密的数字签名
支持邮件多个 tos, ccs, bccs and reply-tos
可以工作在任何服务器平台,所以不用担心win平台无法发送邮件的问题的
支持文本/html格式邮件
可以嵌入image图像
对于邮件客户端不支持html阅读的进行支持
功能强大的发送邮件调试功能debug
自定义邮件header
冗余smtp服务器支持
支持8bit, base64, binary, and quoted-printable 编码
文字自动换行
支持多附件发送功能
支持smtp服务器验证功能
在sendmail, qmail, postfix, gmail, imail, exchange 等平台测试成功
提供的下载文件中,包括内容详细的说明文档及示例说明,所以不用担心难于上手的问题!
phpmailer 非常小巧、简单、方便、快捷
以上资料由jiucool 翻译自phpmailer 官网,转载请注明!

phpmailer的使用(这里以使用gmail smtp发送邮件为例,当然也支持sendmail pop 等其他方式):
首先到http://phpmailer.worxware.com/ 下载最新版本的程序包
下载完成后,找到class.phpmailer.php 、class.smtp.php两个类放到自己的目录下!
然后新建一个php文件这里命名为:phpmail_jiucool.php
phpmail_jiucool.php内容如下:
我直接将邮件发送模块写成一个函数postmail_jiucool_com(),大家使用的时候直接调用该函数即可,函数内容为:

复制代码 代码如下:

function postmail_jiucool_com($to,$subject = "",$body = ""){
//author:jiucool website: //www.jb51.net
//$to 表示收件人地址 $subject 表示邮件标题 $body表示邮件正文
//error_reporting(e_all);
error_reporting(e_strict);
date_default_timezone_set("asia/shanghai");//设定时区东八区
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new phpmailer(); //new一个phpmailer对象出来
$body = eregi_replace("[\]",'',$body); //对邮件内容进行必要的过滤
$mail->charset ="utf-8";//设定邮件编码,默认iso-8859-1,如果发中文此项必须设置,否则乱码
$mail->issmtp(); // 设定使用smtp服务
$mail->smtpdebug = 1; // 启用smtp调试功能
// 1 = errors and messages
// 2 = messages only
$mail->smtpauth = true; // 启用 smtp 验证功能
$mail->smtpsecure = "ssl"; // 安全协议
$mail->host = "smtp.googlemail.com"; // smtp 服务器
$mail->port = 465; // smtp服务器的端口号
$mail->username = "smtp服务器用户名"; // smtp服务器用户名
$mail->password = "smtp服务器密码"; // smtp服务器密码
$mail->setfrom('发件人地址,如admin#jiucool.com #换成@', '发件人名称');
$mail->addreplyto("邮件回复地址,如admin#jiucool.com #换成@","邮件回复人的名称");
$mail->subject = $subject;
$mail->altbody = "to view the message, please use an html compatible email viewer! - from www.jiucool.com"; // optional, comment out and test
$mail->msghtml($body);
$address = $to;
$mail->addaddress($address, "收件人名称");
//$mail->addattachment("images/phpmailer.gif"); // attachment
//$mail->addattachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->send()) {
echo "mailer error: " . $mail->errorinfo;
} else {
echo "message sent!恭喜,邮件发送成功!";
}
}