PHP使用SMTP邮件服务器发送邮件示例
发布时间:2020-12-12 22:34:32 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP使用SMTP邮件服务器发送邮件。供大家参考研究具体如下: 用之前记得先去163注册一个邮箱,然后打开SMTP服务,当然也可以使用QQ邮箱等,但配置信息得改。 如图所示,开启QQ邮箱SMTP服务: 话不多说,直接上代码 email.class.php 定
本篇章节讲解PHP使用SMTP邮件服务器发送邮件。分享给大家供大家参考,具体如下: 用之前记得先去163注册一个邮箱,然后打开SMTP服务,当然也可以使用QQ邮箱等,但配置信息得改。 如图所示,开启QQ邮箱SMTP服务: 话不多说,直接上代码 email.class.php 定义发送邮件的库 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->host_name = "smtp.163.com"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to,$from,$subject = "",$body = "",$mailtype,$cc = "",$bcc = "",$additional_headers = "")
{
$header = "";
$mail_from = $this->get_address($this->strip_comment($from));
$body = mb_ereg_replace("(^|(rn))(.)","1.3",$body);
$header .= "MIME-Version:1.0rn";
if ($mailtype == "HTML") { //邮件发送类型
//$header .= "Content-Type:text/htmlrn";
$header .= 'Content-type: text/html; charset=utf-8' . "rn";
}
$header .= "To: " . $to . "rn";
if ($cc != "") {
$header .= "Cc: " . $cc . "rn";
}
$header .= "From: " . $from . "rn";
// $header .= "From: $from<".$from.">rn"; //这里只显示邮箱地址,不够人性化
$header .= "Subject: " . $subject . "rn";
$header .= $additional_headers;
$header .= "Date: " . date("r") . "rn";
$header .= "X-Mailer:By (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,$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 "
"; //echo $header; return $sent; } /* Private Functions */ function smtp_send($helo,$to,$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 "; } } 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; } } ?> index.php 发送邮件的具体实现 debug = TRUE; //是否显示发送的调试信息
$smtp->sendmail($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);
}
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》 希望本文所述对大家PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |