日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP 发送Email的几种方法

發(fā)布時間:2023/12/9 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP 发送Email的几种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載鏈接:http://blog.009it.com/php/75.html


在php中發(fā)送Email可以直接調用系統(tǒng)的mail()函數來完成,但是前提是你在php.ini文件中對mail都已經配置好了,以下為相關的配置信息:

[mail function] ; For Win32 only. SMTP = localhost smtp_port = 25; For Win32 only. ;sendmail_from = me@example.com; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path =; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters =

Mail 配置選項

?

名稱 默認 描述 可更改

SMTP "localhost" Windows 專用:SMTP 服務器的 DNS 名稱或 IP 地址。 PHP_INI_ALL

smtp_port "25" Windows 專用:SMTP 段口號。自 PHP 4.3 起可用。 PHP_INI_ALL

sendmail_from NULL

Windows 專用:規(guī)定從 PHP 發(fā)送的郵件中使用的 "from"

?地址。

PHP_INI_ALL

sendmail_path NULL Unix 系統(tǒng)專用:規(guī)定sendmail 程序的路徑(通常 /usr/sbin/sendmail 或 /usr/lib/sendmail) PHP_INI_SYSTEM

?

上面寫的很清楚,要是windows的服務器,必須裝好開啟本地的smtp服務器,在linux和unix下,要用到sendmail,所以得配置好sendmail,根據自己的實際情況選擇適當的配置信息。

mail()函數如果郵件的投遞被成功地接收,則返回 true,否則返回 false,語法:

mail($to,$subject,$message,$headers,$parameters)

?

參數 描述

$to 必需。規(guī)定郵件的接收者。

$subject 必需。規(guī)定郵件的主題。該參數不能包含任何換行字符。

$message 必需。規(guī)定要發(fā)送的消息。

$headers 必需。規(guī)定額外的報頭,比如 From, Cc 以及 Bcc。

$parameters 必需。規(guī)定 sendmail 程序的額外參數。

?

簡單的發(fā)封郵件,代碼如下:

<?php$txt = "First line of text\nSecond line of text";// 如果一行大于 70 個字符,請使用 wordwrap()。 $txt = wordwrap($txt,70);// 發(fā)送郵件 mail("somebody@example.com","My subject",$txt); ?>
如果是一封帶HTML格式的Email則代碼如下: <?php$to = "somebody@example.com, somebodyelse@example.com"; $subject = "HTML email";$message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</p> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>John</td> <td>Doe</td> </tr> </table> </body> </html> ";// 當發(fā)送 HTML 電子郵件時,請始終設置 content-type $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// 更多報頭 $headers .= 'From: <webmaster@example.com>' . "\r\n"; $headers .= 'Cc: myboss@example.com' . "\r\n";mail($to,$subject,$message,$headers); ?>

函數很簡單,看似很好用,但是實際上局限性很明顯,必須是在本地的開啟smtp的情況下,而且經過測試成功率不是很高,所以程序中很少有人用到mail()函數來發(fā)郵件。

如果要擴展功能的話,還得重新構造,網上也有好多現成的類可以直接調用來實現,其實就是靠fsockopen來進行連接smtp,下面的代碼是個思路:

<?php /**名稱:smtpsend . class . php功能:郵件發(fā)送類, 實現郵件發(fā)送功能, 可以發(fā)送html郵件;可驗證用戶是否具有發(fā)送權限;可實現從任意服務器郵箱發(fā)送郵件,并非局限于本機。 $smtpserver;SMTP服務器 $smtpserverport;SMTP服務器端口 $smtpuser;SMTP服務器的用戶帳號 $smtppass;SMTP服務器的用戶密碼 $fromMail;SMTP服務器的用戶郵箱 $toMail;發(fā)送給誰 $subject;郵件主題 $content;郵件內容 $mailtype;郵件格式(HTML / TXT), TXT為文本郵件 */class smtpclass {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 = 60; //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, $returnpath = "", $cc = "", $bcc = "", $additional_headers = "") {$mail_from = $this->get_address($this->strip_comment($from));$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);$header = "";if (isset($returnpath) && $returnpath != "") {$header.= "Reply-To:" . $returnpath . "\r\n";}$header.= "MIME-Version:1.0\r\n";if ($mailtype == "HTML") {$header.= 'Content-Type:text/html; charset=gb2312' . "\r\n";}$header.= "To: " . $to . "\r\n";if ($cc != "") {$header.= "Cc: " . $cc . "\r\n";}$header.= "From: $from<" . $from . ">\r\n";$header.= "Subject: " . $subject . "\r\n";$header.= $additional_headers;$header.= "Date: " . date("r") . "\r\n";$header.= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";list($msec, $sec) = explode(" ", microtime());$header.= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";$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 host\n");}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");}//authif ($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("^.+@([^@]+)$", "\1", $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 . "\r\n" . $body);$this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));return TRUE;}function smtp_eom() {fputs($this->sock, "\r\n.\r\n");$this->smtp_debug(". [EOM]\n");return $this->smtp_ok();}function smtp_ok() {$response = str_replace("\r\n", "", fgets($this->sock, 512));$this->smtp_debug($response . "\n");if (!ereg("^[23]", $response)) {fputs($this->sock, "QUIT\r\n");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 . "\r\n");$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 = "\([^()]*\)";while (ereg($comment, $address)) {$address = ereg_replace($comment, "", $address);}return $address;}function get_address($address) {$address = ereg_replace("([ \t\r\n])+", "", $address);$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);return $address;}function smtp_debug($message) {if ($this->debug) {echo $message;}}function senduserMail($smtpserver, $smtpserverport = 25, $smtpuser, $smtppass, $fromMail, $toMail, $subject, $content, $mailtype = 'HTML') {if ($fromMail == "") {die("發(fā)件人不能為空!");}if ($smtpuser == "") {die("用戶名不能為空!");}if ($smtppass == "") {die("用戶密碼不能為空!");}if ($subject == "") {die("標題不能為空!");}if ($toMail == "") {die("收件人不能為空!");}if ($content == "") {die("內容不能為空!");}$smtpserver = $smtpserver; //SMTP服務器$smtpserverport = $smtpserverport; //SMTP服務器端口$smtpuser = $smtpuser; //SMTP服務器的用戶帳號$smtppass = $smtppass; //SMTP服務器的用戶密碼$smtpusermail = $fromMail; //SMTP服務器的用戶郵箱$smtpemailto = $toMail; //發(fā)送給誰$mailsubject = $subject; //郵件主題$mailbody = $content; //郵件內容$mailtype = $mailtype; //郵件格式(HTML/TXT),TXT為文本郵件$this->smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //這里面的一個true是表示使用身份驗證,否則不使用身份驗證.//$smtp->debug = TRUE;//是否顯示發(fā)送的調試信息$this->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);} } //$smtp = new smtpclass(); //$smtp->senduserMail("mail.163.com",25,"用戶名","密碼","xxx@xxx.com.cn","xx@xx.com","測試","測試一下","TXT");?>
以上在網上看到的,其實具體的方法都是大同小異,用的很多的是一個開源的叫phpmailer,很方便的。用搜索引擎搜下然后下載就行。

這里貼出phpmailer的測試代碼:

<?php require ("class.phpmailer.php"); require ("class.smtp.php"); //以下是實例: $mail = new phpmailer(); //建立郵件發(fā)送類 $address = xxx@xxx . com; $mail->IsSMTP(); // 使用SMTP方式發(fā)送 $mail->Host = "mail.xxx.com"; // 您的企業(yè)郵局域名 $mail->SMTPAuth = false; // 啟用SMTP驗證功能 $mail->Username = "xxx"; // 郵局用戶名(請?zhí)顚懲暾膃mail地址) $mail->Password = "xxx"; // 郵局密碼 $mail->From = "xxxx@xxxx.cn"; //郵件發(fā)送者email地址 $mail->FromName = "您的名稱"; $mail->AddAddress("$address", ""); //收件人地址,可以替換成任何想要接收郵件的email信箱,格式是AddAddress("收件人email","收件人姓名") //$mail->AddReplyTo("", ""); //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件 //$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式 $mail->Subject = "PHPMailer測試郵件"; //郵件標題 $mail->Body = "Hello,這是測試郵件"; //郵件內容 $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略 if (!$mail->Send()) {echo "郵件發(fā)送失敗. <p>";echo "錯誤原因: " . $mail->ErrorInfo;exit; } echo "郵件發(fā)送成功"; ?>

總結

以上是生活随笔為你收集整理的PHP 发送Email的几种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。