php 发送邮件与发送邮件类

话不多说,依据官方dome修改下,官方更新地址:https://github.com/PHPMailer/PHPMailer

 代码如下

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
date_default_timezone_set('PRC');//设置邮件发送的时间,如果不设置,则会显示其他区的时间

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //服务器设置
 // 0 = off (for production use)
 // 1 = client messages
 // 2 = client and server messages
 // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
    $mail->SMTPDebug = 2;                                 
    $mail->isSMTP();                                      // 使用SMTP协议进行发送
    $mail->Host = 'smtp.exmail.qq.com';  // SMTP 邮箱服务器
    $mail->SMTPAuth = true;                               // SMTP验证开启
    $mail->Username = 'service@ibang114.com';                 // 用户名,一般填发件箱
    $mail->Password = '';                           // 邮箱密码
    $mail->SMTPSecure = 'SSL';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25;                                    // SMTP端口

    //收件人
    $mail->setFrom('service@ibang114.com', '南昌大学圈');  // 设置发件人邮箱地址
    $mail->addAddress('123456789@qq.com', '尊敬的客户');     // 设置收件人邮箱地址

    //内容
    $mail->isHTML(true);                       // 将电子邮件格式设置为 HTML
 $mail->CharSet = 'UTF-8';
    $mail->Subject = '静夜思';      //邮件的主题
    $mail->Body    = '床前明月光<br />疑是地上霜<br />举头望明月<br />低头思故乡<br />';//邮件的内容


    $mail->send();
    echo '邮件已发送';
 } catch (Exception $e) {
  echo '无法发送邮件。邮件错误: ', $mail->ErrorInfo;
 }
?>

<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=gb2312″
/>
<title>使用php发送电子邮件</title>
<style
type=”text/css教程“>
<!–
.style1 {font-size: 12px}
.style2 {
 font-size: 24px;
 font-weight: bold;
}
–>
</style>
</head>

  

<body>
<p align=”center” class=”style2″>使用php发电子邮件</p>
<form name=”form1″ method=”post” action=”send_mail.php”>
<table width=”444″ height=”347″ border=”0″ align=”center”>
    <tr>
        <td width=”71″ height=”23″ bgcolor=”#d6b1e9″>
        <div align=”right” class=”style1″>
            <div align=”left”> 收件人</div>
        </div>
        </td>
        <td width=”363″>
            <label> <input type=”text”
name=”sendto”></label>
        </td>
    </tr>
    <tr>
        <td height=”27″ bgcolor=”#d6b1e9″>
        <div align=”right” class=”style1″>
            <div align=”left”> 邮件标题</div>
        </div></td>
        <td>
            <label> <input type=”text”
name=”subject”></label>
        </td>
    </tr>
    <tr>
        <td height=”23″ colspan=”2″ bgcolor=”#d6b1e9″>
        <div align=”right” class=”style1″>
            <div align=”left”> 邮件正文</div>
        </div>
        </td>
    </tr>
    <tr>
        <td colspan=”2″ bgcolor=”#d6b1e9″>
        <div align=”right”>www.111cn.net
            <label>
            <div align=”left”>
                <textarea name=”emailcontent” cols=”60″
rows=”18″></textarea>
            </div>
            </label>
        </div>
          <div align=”right”></div>
        <div align=”right”></div>
        </td>
    </tr>
    <tr>
        <td colspan=”2″>
            <label><input type=”submit” name=”submit”
value=”提交”></label>
        </td>
    </tr>
</table>
</form>

</body>
</html>

 

 

send_mail.php

 

 代码如下

<?php
include “mail.php”;

if(isset($_post[‘sendto’]))
    $sendto = $_post[‘sendto’];
else
    $sendto = ”;

if(isset($_post[‘subject’]))
    $subject = $_post[‘subject’];
else
    $subject = ”;

if(isset($_post[’emailcontent’]))
    $emailcontent = $_post[’emailcontent’];
else
    $emailcontent = ”;

if(empty($sendto) || empty($subject) || empty($emailcontent))
{
    echo
‘<b>请填入收件人email地址、邮件主旨和邮件正文。</b><br/>’;
    exit;
}

$sendmail=new smtp_mail(“smtp.sina.com”, “sina”, false);
if($sendmail->send(“macbooks@sina.com“, $sendto, $subject,
$emailcontent))
{
    echo “发送成功!”;
}
else
{
    echo “www.111cn.net发送失利!”;
}
?>

 

 

//mail.php

 

 代码如下

<?php
class smtp_mail
{
    var $lastmessage;                           
//记录最终回来的响应音信
    var $lastact;                               
//最终的动作,字符串格局
    var $welcome;                               
//用在helo前面,欢迎用户
    var $debug;                                  //是还是不是出示调试消息
    var $smtp;                                   //smtp服务器
    var $port;                                   //smtp端口号
    var $fp;                                     //socket句柄
   
    function smtp_mail($smtp, $welcome=””, $debug=false)
    {
        if(empty($smtp)) die(“smtp cannt be null!”);
       
        $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)
    {
        $b = false;
        if ($this->debug)
        {
            if($inout==”in”)                     //响应消息
            {
                $m='<< ‘;
                $b = true;
            }
            else                                 //请求指令
                $m=’>> ‘;
            if(!ereg(” $”, $message))
                $message .= “”;
            $message=nl2br($message);
            if($b)
                echo “<b>${m}${message}</b>”;
            else
                echo “${m}${message}”;
        }
    }
   
    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($from,$to,$subject,$message)
    {
        $this->show_debug(“connect to smtp server :
“.$this->smtp.” “, “out”);
        $this->fp = fsockopen ( $this->smtp, $this->port );
        if ( $this->fp )
        {
            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 . ” “;
                if(!$this->do_command($this->lastact, “250”))
                {
                    fclose($this->fp);
                    return false;
                }
               
                $this->lastact=”mail from:<$from>”. ” “;
                if(!$this->do_command($this->lastact, “250”))
                {
                    fclose($this->fp);
                    return false;
                }
           
                $this->lastact=”rcpt to:<$to>” . ” “;
                if(!$this->do_command($this->lastact, “250”))
                {
                    fclose($this->fp);
                    return false;
                }

                $this->lastact=”data “;         //发送正文
                if(!$this->do_command($this->lastact, “354”))
                {
                    fclose($this->fp);
                    return false;
                }

                $head=”subject: $subject “;     //处理subject
                if(!empty($subject) && !ereg($head, $message))
                {
                    $message = $head.$message;
                }
           
               
                $head=”from: $from “;           //处理from
                if(!empty($from) && !ereg($head, $message))
                {
                    $message = $head.$message;
                }

                $head=”to: $to “;               //处理to
                if(!empty($to) && !ereg($head, $message))
                {
                    $message = $head.$message;
                }

                if(!ereg(” . “, $message))    //加上得了串
                    $message .= ” . “;
                $this->show_debug($message, “out”);
                fputs($this->fp, $message);
           
                $this->lastact=”quit “;
                if(!$this->do_command($this->lastact, “250”))
                {
                    fclose($this->fp);
                    return false;
                }
            }
            return true;
        }
        else
        {
            $this->show_debug(“connect failed!”, “in”);
            return false;
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图