Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 linux下php配置smtp发送邮件的方法

linux下php配置smtp发送邮件的方法

发布时间:2017-10-08   编辑:www.jquerycn.cn
本文为大家介绍linux下smtp配置的几种方法,供大家学习参考。

方法一,使用mail函数发送邮件;使用时时需要在本地系统上正确设置SMTP,否则将不能发送邮件。由于对系统的依赖性比较大,很多时候很不稳定,在一些提供虚拟主机服务的代理商中使用mail函数发送邮件往往很不好用,所以不推荐使用这种方法。

方法二,使用管道的形式发送邮件,主要是使用php中的popen函数。使用管道的方法发送邮件属于比较底层的操作,它取决于用户调用程序的稳定性。所以相比mail函数,这是一种可选的发送邮件的方式,但是这些本地的邮件系统都太复杂了,用户可能不会配置。

方法三(推荐),使用phpmailer。phpmailer类是一个开源的发送邮件类,可以从http://phpmailer.sourceforge.net官网下载,它含两个文件class.smtp.php和class.phpmailer.php。 
 

复制代码 代码如下:

<?php
include_once("class.phpmailer.php");
/**
 * 定义邮件模块配制信息
 */
define("SMTP_HOST","smtp.mail.yahoo.com");            // SMTP 主机
define("SMTP_MAIL"," XXXX@yahoo.cn");        // SMTP 用户email
define("SMTP_PASS"," XXXX");                          // SMTP 用的密码

define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP 用户email
define("SERVICE_NAME","PHPBOOK邮件测试"); // SMTP 用的名字

/**
     * 使用phpmailer发邮件模块
     *
     * @param string $email
     * @param string $user
     * @param string $subject
     * @param string $body
     * @return bool
     */
function sendMail($email,$user,$subject,$body)
{
    $mail = new PHPMailer();
   //$this;
    $mail->IsSMTP();                                // 设置使用SMTP
    $mail->Host = SMTP_HOST;                      // 设置SMTP服务器地址
    $mail->SMTPAuth = true;                         // 打开SMTP权限验证
    $mail->Username = SMTP_MAIL;                      // SMTP 用户名
    $mail->Password = SMTP_PASS;                     // SMTP 服务器密码

    $mail->From = SERVICE_MAIL;                        // 设置发送者地址
    $mail->FromName = SERVICE_NAME;                    // 设置发送者名字
    $mail->AddAddress($email, $user);                // 添加接收者地址
    $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME);     // 设置回复地址

    $mail->WordWrap = 50;                // 设置显示格式
    $mail->IsHTML(true);                 // 设置邮件支持html
    $mail->Subject = $subject;
    $mail->Body    = $body;
    $mail->AltBody = "";                // 文本类型的邮件

    if(!$mail->Send())
    {
        return $mail->ErrorInfo;
    }
    return true;
}

//开始发送测试邮件ng: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
$tomail = " XXXX@126.com";
$user = " XXXXlinux";
$_mailSubject = "邮件测试示例!"; // 发给用户的邮件标题小组
$_mailBody = "<a href='http://business.sina.com/' style='color:red'>新浪网</a>"; // 邮件内容小组
sendMail($tomail,$user,$_mailSubject,$_mailBody);
?>

实验证明yahoo的smtp很好用,号称sina的其实并不好用,我卡在着好长时间。

方法四,给予socket编写的程序
使用socket发送邮件的封装类:
 

复制代码 代码如下:
<?php
class sendmail{
    var $lastmessage; //记录最后返回的响应信息
    var $lastact;     //最后的动作,字符串形式
    var $welcome;     //用在HELO后面,欢迎用户
    var $debug;     //是否显示调试信息
    var $smtp;         //smtp服务器
    var $port;         //smtp端口号
    var $fp;         //socket句柄
    //发送邮件函数
    function send_mail($smtp, $welcome="", $debug=false) {
        if(empty($smtp)) die("SMTP不能为空!");
        $this->smtp=$smtp;
        if(empty($welcome)) {
            $this->welcome=gethostbyaddr("localhost");
        }else
        $this->welcome=$welcome;
        $this->debug=$debug;
        $this->lastmessage="";
        $this->lastact="";
        $this->port="25";
    }
    //显示调试信息
    function show_debug($message, $inout) {
        if ($this->debug) {
            if($inout=="in"){ //响应信息
                $m='<< ';
            }else
            $m='>> ';
            if(!ereg("\n$", $message))
            $message .= "<br>";
            $message=nl2br($message);
            echo "<font color=#99FF99>${m}${message}</font>";
        }
    }
    //执行传递的命令
    function do_command($command, $code) {
        $this->lastact=$command;
        $this->show_debug($this->lastact, "out");
        fputs ( $this->fp, $this->lastact );
        $this->lastmessage = fgets ( $this->fp, 512 );
        $this->show_debug($this->lastmessage, "in");
        if(!ereg("^$code", $this->lastmessage))
        return false;
        else
        return true;
    }
    //邮件发送处理
    function send( $to,$from,$subject,$message) {
        //连接服务器
        $this->lastact="connect";
        $this->show_debug("连接到SMTP 服务器: ".$this->smtp, "out");
        $this->fp = fsockopen ( $this->smtp, $this->port );
        if ( $this->fp ) {
            $this->set_socket_blocking( $this->fp, true );
            $this->lastmessage=fgets($this->fp,512);
            $this->show_debug($this->lastmessage, "in");
            if (! ereg ( "^220", $this->lastmessage ) ) {
                return false;
            }else{
                $this->lastact="HELO " . $this->welcome . "\n";
                if(!$this->do_command($this->lastact, "250")){
                    fclose($this->fp);
                    return false;
                }
                $this->lastact="MAIL FROM: $from" . "\n";
                if(!$this->do_command($this->lastact, "250")){
                    fclose($this->fp);
                    return false;
                }
                $this->lastact="RCPT TO: $to" . "\n";
                if(!$this->do_command($this->lastact, "250")){
                    fclose($this->fp);
                    return false;
                }
                //开始发送邮件正文
                $this->lastact="DATA\n";
                if(!$this->do_command($this->lastact, "354")){
                    fclose($this->fp);
                    return false;
                }
                //开始处理邮件主题头
                $head="Subject: $subject\n";
                if(!empty($subject) && !ereg($head, $message)){
                    $message = $head.$message;
                }
                //开始处理邮件From头
                $head="From: $from\n";
                if(!empty($from) && !ereg($head, $message)) {
                    $message = $head.$message;
                }
                //开始处理邮件To头
                $head="To: $to\n";
                if(!empty($to) && !ereg($head, $message)) {
                    $message = $head.$message;
                }
                //处理结束串
                if(!ereg("\n\.\n", $message))
                $message .= "\n.\n";
                $this->show_debug($message, "out");
                fputs($this->fp, $message);
                $this->lastact="QUIT\n";
                if(!$this->do_command($this->lastact, "250")){
                    fclose($this->fp);
                    return false;
                }
            }
            return true;
        }else{
            $this->show_debug("连接失败!!", "in");
            return false;
        }
    }
}
?>

使用socket发送邮件示例:
 

复制代码 代码如下:
<?php
include ("./sendmail.class.php");
$mail = new sendmail();
$email = "您好,这是一个测试邮件!";
$sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //显示调示信息
if($mail->send("XXXX@126.com", "XXXX@126.com", "测试SOCKET邮件", $email)) {
    echo "发送成功!<br>";
}else{
    echo "发送失败!<br>";
}
?>

您可能感兴趣的文章:
php 邮件发送类(smtp方式或mail函数方式)
php使用smtp发送邮件的实现代码
php smtp发送邮件的函数
php中通过curl smtp发送邮件的例子
php使用Pear的NetMail发送smtp邮件
使用pear:Net_SMTP类发送邮件的例子
php写的smtp邮件发送类

您可能感兴趣的文章:
php smtp发送邮件的函数
《Perl编程24学时教程》笔记第22课 CGI发送电子邮件
phpmailer邮件发送实例(163邮箱 126邮箱 yahoo邮箱)
phpmailer发送yahoo邮件的例子
使用pear:Net_SMTP类发送邮件的例子
php使用smtp发送邮件的实现代码
用PHP发电子邮件
PHPMailer邮件类发送邮件举例(163邮箱)
PHPMailer发送邮件代码实例(ubuntu系统)
phpmailer发送网易126邮箱的例子

关键词: smtp  发送邮件   
[关闭]