php 发送邮件代码程序

 更新时间:2016年11月25日 17:02  点击:1686

php 发送邮件代码程序

<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file ="";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(\.)", "[url=file://\1.\3]\1.\3[/url]", $body);
$header .= "MIME-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/htmlrn ";
}
$header .= "To: ".$to."rn";
if ($cc != "") {
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $from<".$from.">rn";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote hostn");
}
echo "<br>";
echo $header;
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending HELO command");
}
}
#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]", "[url=file://\1]\1[/url]", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX "".$domain.""n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Warning: Cannot connect to mx host ".$host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
continue;
}
$this->log_write("Connected to mx host ".$host."n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned "".$response.""n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "[url=file://\([^()]*\]\([^()]*\[/url])";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "[url=file://\1]\1[/url]", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message."<br>";
}
}
function get_attach_type($image_tag) { //
$filedata = array();
$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);
$filedata['context'] = $image_data;
$filedata['filename']= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
switch($extension){
case ".gif":
$filedata['type'] = "image/gif";
break;
case ".gz":
$filedata['type'] = "application/x-gzip";
break;
case ".htm":
$filedata['type'] = "text/html";
break;
case ".html":
$filedata['type'] = "text/html";
break;
case ".jpg":
$filedata['type'] = "image/jpeg";
break;
case ".tar":
$filedata['type'] = "application/x-tar";
break;
case ".txt":
$filedata['type'] = "text/plain";
break;
case ".zip":
$filedata['type'] = "application/zip";
break;
default:
$filedata['type'] = "application/octet-stream";
break;
}

return $filedata;
}
}
?>
send.php:
$title                   = $_POST['title'];
$Message            = $_POST['Message'];
$obj_title           = iconv('UTF-8','Shift-JIS',$title);
$obj_message    = iconv('UTF-8', 'Shift-JIS',$Message);
$smtpserver     = "smtp.163.com";//SMTP服务器
  $smtpserverport =25;//SMTP服务器端口
  $smtpusermail   = "";//SMTP服务器的用户邮箱
  $smtpemailto    = "";//发送给谁
  $smtpuser       = "";  //SMTP服务器的用户帐号
  $smtppass       = "";//SMTP服务器的用户密码
  $mailsubject    = $title;//邮件主题
  $mailbody       = $Message  ;//邮件内容
  $mailtype       = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  $smtp           = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = true;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

这个例子说明如何发送电子邮件使用SMTP身份验证。此特定示例使用Gmail发送。为通过SMTP发送如需Gmail要求的SMTP TLS身份验证。幸运的是,当我们使用梨邮件认证,连接自动TLS的。

这个例子之间的区别,只是在前面的示例使用SMTP是增加以下SMTP参数。

$smtp_params["auth"]     = true;
$smtp_params["username"] = "user@gmail.com";
$smtp_params["password"] = "pass";
<?
        include('Mail.php');
        include('Mail/mime.php');
 
        // Constructing the email
        $sender = "user@gmail.com";                                             // Your email address
        $recipient = "Leigh <leigh@no_spam.net>";                               // The Recipients name and email address
        $subject = "Test Email";                                                // Subject for the email
        $text = 'This is a text message.';                                      // Text version of the email
        $html = '<html><body><p>This is a html message</p></body></html>';      // HTML version of the email
        $crlf = "n";
        $headers = array(
                        'From'          => $sender,
                        'Return-Path'   => $sender,
                        'Subject'       => $subject
                        );
 
        // Creating the Mime message
        $mime = new Mail_mime($crlf);
 
        // Setting the body of the email
        $mime->setTXTBody($text);
        $mime->setHTMLBody($html);
 
        // Add an attachment
        $file = "Hello World!";
        $file_name = "Hello text.txt";
        $content_type = "text/plain";
        $mime->addAttachment ($file, $content_type, $file_name, 0);
 
        // Set body and headers ready for base mail class
        $body = $mime->get();
        $headers = $mime->headers($headers);
 
        // SMTP authentication params
        $smtp_params["host"]     = "smtp.gmail.com";
        $smtp_params["port"]     = "25";
        $smtp_params["auth"]     = true;
        $smtp_params["username"] = "user@gmail.com";
        $smtp_params["password"] = "pass";
 
        // Sending the email using smtp
        $mail =& Mail::factory("smtp", $smtp_params);
        $result = $mail->send($recipient, $headers, $body);
        if($result === 1)
        {
          echo("Your message has been sent!");
        }
        else
        {
          echo("Your message was not sent: " . $result);
        }
?>

<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file ="";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(\.)", "[url=file://\1.\3]\1.\3[/url]", $body);
$header .= "MIME-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/htmlrn ";
}
$header .= "To: ".$to."rn";
if ($cc != "") {
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $from<".$from.">rn";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote hostn");
}
echo "<br>";
echo $header;
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending HELO command");
}
}
#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]", "[url=file://\1]\1[/url]", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX "".$domain.""n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Warning: Cannot connect to mx host ".$host."n");
$this->log_write("Error: ".$errstr." (".$errno.")n");
continue;
}
$this->log_write("Connected to mx host ".$host."n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned "".$response.""n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "[url=file://\([^()]*\]\([^()]*\[/url])";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "[url=file://\1]\1[/url]", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message."<br>";
}
}
function get_attach_type($image_tag) { //
$filedata = array();
$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);
$filedata['context'] = $image_data;
$filedata['filename']= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
switch($extension){
case ".gif":
$filedata['type'] = "image/gif";
break;
case ".gz":
$filedata['type'] = "application/x-gzip";
break;
case ".htm":
$filedata['type'] = "text/html";
break;
case ".html":
$filedata['type'] = "text/html";
break;
case ".jpg":
$filedata['type'] = "image/jpeg";
break;
case ".tar":
$filedata['type'] = "application/x-tar";
break;
case ".txt":
$filedata['type'] = "text/plain";
break;
case ".zip":
$filedata['type'] = "application/zip";
break;
default:
$filedata['type'] = "application/octet-stream";
break;
}

return $filedata;
}
}
?>
send.php:
$title                   = $_POST['title'];
$Message            = $_POST['Message'];
$obj_title           = iconv('UTF-8','Shift-JIS',$title);
$obj_message    = iconv('UTF-8', 'Shift-JIS',$Message);
$smtpserver     = "smtp.163.com";//SMTP服务器
  $smtpserverport =25;//SMTP服务器端口
  $smtpusermail   = "";//SMTP服务器的用户邮箱
  $smtpemailto    = "";//发送给谁
  $smtpuser       = "";  //SMTP服务器的用户帐号
  $smtppass       = "";//SMTP服务器的用户密码
  $mailsubject    = $title;//邮件主题
  $mailbody       = $Message  ;//邮件内容
  $mailtype       = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  $smtp           = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = true;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

本文章是利用phpmailer来实现在线发送邮件功能的源码代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>--邀请好友参加</title>
<style type="text/css">
<!--
body {
 margin: 0px;
 padding: 0px;
 font-size: 12px;
 background-color: #CFCFCF;
}
input {
 line-height: 18px;
 height: 18px;
 border: 1px solid #CCCCCC;
}
img {
 border-top-width: 0px;
 border-right-width: 0px;
 border-bottom-width: 0px;
 border-left-width: 0px;
}
* {
 margin: 0px;
 padding: 0px;
 list-style-image: none;
 list-style-type: none;
 background-repeat: no-repeat;
}
td {
 line-height: 22px;
 font-size: 14px;
 font-weight: 700;
 color: #276662;
}
-->
</style>
</head>

<body>

<form action="#" method="post" name="form">
<table border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="572" height="227" valign="top" background="img/mailfooterimg.gif"><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td height="70">&nbsp;</td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><input type="text" value="http://111cn.net/" size="60" /></td>
            <td height="40"><a href="#"><img src="img/copy.gif" width="72" height="22" /></a></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td height="28">好友姓名:
          <input type="text" size="22" />
          &nbsp;&nbsp;
          邮箱地址:
          <input type="text" size="22" />        </td>
      </tr>
      <tr>
        <td height="28">好友姓名:
          <input type="text" size="22" />
          &nbsp;&nbsp;
          邮箱地址:
          <input type="text" size="22" />        </td>
      </tr>
      <tr>
        <td height="28">好友姓名:
          <input type="text" size="22" />
          &nbsp;&nbsp;
          邮箱地址:
          <input type="text" size="22" />        </td>
      </tr>
      <tr>
        <td height="28" align="right"><a href="#"><img src="img/sendbtn.gif" width="95" height="26" /></a></td>
      </tr>
    </table></td>
  </tr>
</table>
</form>
</body>
</html>
发送邮件处理功能页面。

mail.php

<?

require(dirname(__FILE__)."/mail/class.phpmailer.php"); //调用 phpmailer类型,如果没有phpmailer请点击这里下载phpmailer for php5/6 下载

$array =  array_unique(Get_value('mail',1));//去除重复的邮箱地址

$mail = new PHPMailer(); 
 $count =0; 
 $bad =0;
 $mail->IsSMTP();                                      // set mailer to use SMTP
 $mail->Host = "smtp.163.com";  // smtp1.example.com;smtp2.example.comspecify main and backup server
 $mail->SMTPAuth = true;     // turn on SMTP authentication
 $mail->Username = "mailangel123";  // SMTP username
 $mail->Password = "*******"; // SMTP password
 
 $mail->From = "mailangel123@163.com";
 $mail->FromName = "你的好友来信";
 $MailBody = '内容'

$mail->AddReplyTo("mailangel123@163.com", "澳优");
   $mail->AddAddress($tmpmail,'您好!');
   $mail->WordWrap = 50;
   $mail->CharSet="GB2312";                                
   //$mail->AddAttachment("/var/tmp/file.tar.gz");       
   //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");  
   $mail->IsHTML(true);                           
   
   $mail->Subject = "你的朋友邀请你一起合影!";
   $mail->Body    = $MailBody;
   
   if(!$mail->Send())
   {
      $bad++;
      $mail->ClearAddresses();  
      $mail->ClearAttachments();
     
   }

OK就完成了哦。

?>

我们利用 phpmailer功能实现 邮件发送功能哦,这里还利用了模板呢,就是读取指定文件内容再发送给朋友。

<?php
@session_start(); 
 include(dirname(__FILE__).'./inc/function.php');
 require(dirname(__FILE__)."/mail/class.phpmailer.php"); 
 $array =  array_unique(Get_value('mail',1));
 $type = Get_value('type',1);

 
 $mail = new PHPMailer(); 
 $count =0; 
 $bad =0;
 $mail->IsSMTP();                                      // set mailer to use SMTP
 $mail->Host = "smtp.163.com";  // smtp1.example.com;smtp2.example.comspecify main and backup server
 $mail->SMTPAuth = true;     // turn on SMTP authentication
 $mail->Username = "mailangel123";  // SMTP username
 $mail->Password = "******"; // SMTP password
 
 $mail->From = "mailangel123@163.com";
 $mail->FromName = "你的好友来信";
 $MailBody = GetContent($type);

 //$array =explode('|',$rs['mail']);
 foreach( $array as $tmpmail ){
  if( @preg_match("/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/",$tmpmail)
   || strlen($User_Mail)<6 )
  {
   $mail->AddReplyTo("mailangel123@163.com", "44");
   $mail->AddAddress($tmpmail,'您好!');
   $mail->WordWrap = 50;
   $mail->CharSet="GB2312";                                
   //$mail->AddAttachment("/var/tmp/file.tar.gz");       
   //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");  
   $mail->IsHTML(true);                           
   
   $mail->Subject = "你的朋友邀请你一起合影!";
   $mail->Body    = $MailBody;
   
   if(!$mail->Send())
   {
      $bad++;
      $mail->ClearAddresses();  
      $mail->ClearAttachments();
     
   }
   else
   {
    $count++;
   }
  }
  ShowMsg("result:$count");
  
 }

下面这个文章是读取html 文档并进行html发送哦,
 
 function GetContent($type){
  if( $type )
  {
   if(file_exists('./mail_room.html') )
   {
    $content = file_get_contents( './mail_room.html');
   }
   else
   {
    ShowMsg('file can' read fail ');
   }
  }
  else
  {
   if( file_exists( './mail_person.html') )
   {
    $content = file_get_contents( './mail_person.html');
   }
   else
   {
    ShowMsg('person file read fail!');
   }
   
  }
  return $content;
 }
 
 
 /* echo "<script>alert('发关".$count."邮件成功');</script>"; */

?>
本站原创http://www.111cn.net 转载请注明

[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
  • 微信小程序实现点击导航条切换页面

    这篇文章主要为大家详细介绍了微信小程序实现点击导航条切换页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-19
  • 微信小程序手势操作之单触摸点与多触摸点

    这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
  • 微信小程序实现canvas分享朋友圈海报

    这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
  • Python爬取微信小程序通用方法代码实例详解

    这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29