⑴ 如何使用php通過SMTP發送電子郵件
使用PHP類可以實現,例如:phpmailer,下面是示例代碼
require("class.phpmailer.php");//這個是一個smtp的php文檔,網上可以下載得到
$mail=newPHPMailer();//建立郵件發送類
$mail->CharSet="UTF-8";
$address="[email protected]";
$mail->IsSMTP();//使用SMTP方式發送
$mail->Host="smtp.126.com";//您的企業郵局域名
$mail->SMTPAuth=true;//啟用SMTP驗證功能
$mail->Username="[email protected]";//郵局用戶名(請填寫完整的email地址)
$mail->Password="123444";//郵局密碼
$mail->Port=25;
$mail->From="[email protected]";//郵件發送者email地址
$mail->FromName="在線Q聊";
$mail->AddAddress("$address","a");//收件人地址,可以替換成任何想要接收郵件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("","");
//$mail->AddAttachment("/var/tmp/file.tar.gz");//添加附件
$mail->IsHTML(true);//setemailformattoHTML//是否使用HTML格式
$mail->Subject="在線Q聊";//郵件標題
$mail->Body="在線Q聊網站歡迎訪問";//郵件內容,上面設置HTML,則可以是HTML
if(!$mail->Send())
{
echo"郵件發送失敗.<p>";
echo"錯誤原因:".$mail->ErrorInfo;
exit;
}
參見:http://www.uedsc.com/phpmailer-demo.html
⑵ 如何在 BlueHost 空間中用 PHP 以 SMTP 方式發送郵件
你可以用 mail,sendmail 和 smtp 三種方式來藉助 Mail 類發送郵件。話說幾天前剛買了個 BlueHost 的空間,在上面跑程序的時候用 PHP 發郵件搞出點心得來,放在這里跟大家分享。 首先我在系統中添加了一個郵件賬戶 test,密碼 123456,然後寫下了如下代碼: require_once( 'Mail.php'); $conf = array( 'host' => 'mail.yourdomail.com', 'auth' => true, 'username' => 'test', 'password' => '123456' ); $headers['From'] = '[email protected]'; $headers['To'] = $To; $headers['Subject'] = '=?GB2312?B?' . base64_encode( $Subject) . '?='; $headers['Content-Type'] = 'text/plain; charset=gb2312'; $Mail = & Mail::factory( 'smtp', $conf); $res = $Mail->send( $To, $headers, $Content); ?> $To 是想要發送到的信箱地址,比如 [email protected],$Subject 是郵件主題,$Content 是 GB2312 編碼的郵件正文。結果用以上程序屢試屢敗,後來經過多次試驗才得出在 BlueHost 主機上用 PHP 以 SMTP 方式發送郵件的正確方法,注意事項如下: 1) BlueHost 幫助信息中說跟你賬戶(域名)相對應的 SMTP 主機地址是 mail.yourdomain.com,但其實應該是 localhost。你用 mail.yourdomain.com 只會得到 SMTP 拒絕鏈接的錯誤信息。 2) SMTP 伺服器需要驗證才能發郵件。 3) SMTP 驗證時用戶名需要寫 user+yourdomain.com,不能寫 [email protected]。想想這個也好理解,在虛擬主機上一個 SMTP server 要支撐很多個賬戶,這種寫法應該是為了區別發件的賬戶。 4) 郵件頭中的發件郵件賬戶一定要是存在於系統中的真實賬戶,象我上面那樣注冊了一個 [email protected],但是發件人賬戶又寫 [email protected] 是不行的,只會得到這樣的錯誤信息: SMTP: Invalid response code received from server (code: 550, response: Verification failed for No Such User Here Sender verify failed) 知道了上面這 4 個注意事項以後就好辦了,刪掉剛才注冊的 test 郵件帳號,注冊一個 noreply 帳號,修改程序如下: require_once( 'Mail.php'); $conf = array( 'host' => 'localhost', 'auth' => true, 'username' => 'noreply+yourdomain.com', 'password' => '123456' ); $headers['From'] = '[email protected]'; $headers['To'] = $To; $headers['Subject'] = '=?GB2312?B?' . base64_encode( $Subject) . '?='; $headers['Content-Type'] = 'text/plain; charset=gb2312'; $Mail = & Mail::factory( 'smtp', $conf); $res = $Mail->send( $To, $headers, $Content); ?> 郵件被成功的發送出去了,而且由於郵件伺服器在美國,發送到 GMail 信箱非常之快,一般這邊一發那邊刷新一下就能看到新郵件:) 這里再說些題外話,買了 BlueHost 的空間將近一周時間了,感覺美國的虛擬主機市場確實非常成熟,功能強大。跟國內價格還行但是配置超受限的主機比起來性價比高的不得了!只要你選對了服務提供商,可以說絕對讓你滿意。但是缺點也比較明顯,就是速度實在是慢!這裡面的原因眾所周知,感謝偉大的電信運營商,感謝偉大的 GFW! 今天跟 HostMonster 的客戶溝通了一下,問問他們那邊從中國過去的訪問速度怎麼樣,估計人家遇到這樣的情況不是個別現象,上來就是一句,我們對來自中國的訪問速度無法做出承諾。 國內的虛擬主機服務商有萬網這樣把大家當 SB 漫天要價的主,但是也有兢兢業業踏踏實實做服務的公司,不過性價比跟國外的運營商確實沒法比。一邊是價格高不了多少,動輒幾十 G 空間幾百 G 月流量,支持 SSH,支持多 Addon domain 的國外主機,一邊是價廉物不太美的國內主機,我想誘惑力是不言而喻的。但是速度,唉,傷心是一種說不出的痛!
⑶ PHP項目使用smtp類,如何設置發件人名稱
下載一個PHPMailer來用吧,下面是代碼:
require_once "/phpmailer/class.phpmailer.php";
// 實例化 PHPMailer 類
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Subject = "XXX"; // 郵件主題
$mail->Body = $message_body; //郵件內容
$mail->Host = "smtp.163.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username"; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail->Password = "password"; // SMTP password
$mail->From = "[email protected]"; // 發件人郵箱
$mail->FromName = "發件人"; // 發件人
$mail->CharSet = "UTF-8"; // 這里指定字元集
$mail->Encoding = "base64";
$mail->IsHTML(true); // send as HTML
// 填入最基本的參數
$mail->AddAddress( "[email protected]" ); // 收件人
$mail->Send();
⑷ php郵件發送類smtp.class.php在伺服器上發送失敗
開啟php配置文件的兩個擴展:extension=php_sockets.dll和extension=php_openssl.dll,將前面的兩個分號去掉就行。
這兩個函數呢 然後重啟服務
⑸ PHP郵件發送類怎麼弄,我的出錯了,幫我看下
if(mysql_insert_id()){//寫入成功,發郵件
跟這個語句對於的大括弧呢??
你這個錯誤很明顯,告訴你是缺少閉合結束符,一般就是指大括弧
⑹ php smtp發送郵件
哈哈 163 的郵箱 好像 得 07年之前注冊的可以 要不就是 06年之前的 我忘了 我曾經弄過 哈哈
⑺ 請問一下,PHP配置SMTP怎麼弄
PHPMailer的獲取:
PHPMailer項目地址:PHPMailer 使用git命令克隆到本地,或直接在該項目頁面的右下方點擊「 Download ZIP 」即可獲取到完整的PHPMailer代碼包,再到本地解壓即可。
步驟一:使我們的QQ郵箱能夠發送郵件
這里怎麼說能夠發送郵件呢?其實我們的郵箱都是可以發送郵件的,但是要實現在我們的網站中發送郵件,那就要設置一下我們的QQ郵箱了,因為此時我們的網站現在是作為一個第三方客戶端存在的。
這里怎麼說能夠發送郵件呢?其實我們的郵箱都是可以發送郵件的,但是要實現在我們的網站中發送郵件,那就要設置一下我們的QQ郵箱了,因為此時我們的網站現在是作為一個第三方客戶端存在的
由於待會我們用到的是SMTP伺服器來發送,在這里建議把前面的兩項開啟了!當你點擊開啟的時候,它會提示:
<?phprequire_once("./functions.php");$flag=sendMail('[email protected]','lsgo在線通知','恭喜你成功加入LSGO實驗室,開啟你的學習之旅吧!');if($flag){echo"發送郵件成功!";
}else{echo"發送郵件失敗!";
}?>
⑻ php發送郵件的服務類出現錯誤smtp_connect_failed,怎麼解決
郵件伺服器連接失敗,仔細檢查你發郵件的郵箱地址和密碼是否正確,檢查郵箱是否開通STMP
⑼ 如何使用php通過smtp發送郵件步驟
由於php沒有提供現成的smtp函數,卻提供了一個功能不甚靈活的mail()函數,這個函數需要伺服器配置上的支持,並且不支持smtp驗證,在很多場合無法正常的工作,因此不建議使用。
首先是使用telnet來連接本地的25埠,稍微熟悉點網路的人都知道smtp協議使用25埠,這也就是說,現在在連接本地的smtp伺服器。
<?php
require_once'Mail.php';
$conf['mail']=array(
'host'=>'xx.xx.xx.xx',//smtp伺服器地址,可以用ip地址或者域名
'auth'=>true,//true表示smtp伺服器需要驗證,false代碼不需要
'username'=>'tester',//用戶名
'password'=>'retset'//密碼
);
/***
*使用$headers數組,可以定義郵件頭的內容,比如使用$headers['Reply-To']可以定義回復地址
*通過這種方式,可以很方便的定製待發送郵件的郵件頭
***/
$headers['From']='[email protected]';//發信地址
$headers['To']='[email protected]';//收信地址
$headers['Subject']='testmailsendbyphp';//郵件標題
$mail_object=&Mail::factory('smtp',$conf['mail']);
$body=<<<MSG//郵件正文
helloworld!!!
MSG;
$mail_res=$mail_object->send($headers['To'],$headers,$body);//發送
if(Mail::isError($mail_res)){//檢測錯誤
die($mail_res->getMessage());
}
?>
⑽ php怎麼用smtp實現發送郵件需要代碼!
<?
set_time_limit(120); 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;var $sender;/* Private Variables */
var $sock;/* Constractor */function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass,$sender)
{
$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->sender = $sender;$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";$this->logs = ""; //記錄跟伺服器的交互過程$this->sock = FALSE;
}/* Main Function */function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$sent = TRUE;
$mail_from = $this->get_address($this->strip_comment($from));$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);$header .= "MIME-Version:1.0\r\n";if($mailtype=="HTML"){$header .= "Content-Type:text/html\r\n";
}$header .= "To: ".$to."\r\n";if ($cc != "") {$header .= "Cc: ".$cc."\r\n";}//$header .= "From: $from<".$from.">\r\n";$header .= "From: ".$from."\r\n";$header .= "Subject: ".$subject."\r\n";$header .= $additional_headers;$header .= "Date: ".date("r")."\r\n";$header .= "X-Mailer: 72e.net (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)));}
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 AUTH command");}if (!$this->smtp_putcmd("", base64_encode($this->pass))) { return $this->smtp_error("sending AUTH command");}}#//if (!$this->smtp_putcmd("MAIL", "FROM:".$from."")) {
if (!$this->smtp_putcmd("MAIL", "FROM:<".$this->sender.">")) {
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));//echo "response=".$response."\r\n";$this->smtp_debug($response."\n");//echo "ereg 23 ==".ereg("^[23]", $response)."\n";if (!ereg("^[23]", $response)) {//echo "@@@@@";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");//echo "cmd=".$cmd."\r\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->logs .= $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;}}} // end class
?>
mailtest.php
=====================================================================================================================
<?
include "smtp.class.php";
$smtpserver = "mail.domain.net";//您的smtp伺服器的地址
$port = 25 ; //smtp伺服器的埠,一般是 25
$smtpuser = "[email protected]"; //您登錄smtp伺服器的用戶名
$smtppwd = "test"; //您登錄smtp伺服器的密碼
$mailtype = "HTML"; //郵件的類型,可選值是 TXT 或 HTML ,TXT 表示是純文本的郵件,HTML 表示是 html格式的郵件
$sender = "[email protected]"; //發件人,一般要與您登錄smtp伺服器的用戶名($smtpuser)相同,否則可能會因為smtp伺服器的設置導致發送失敗
$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
$smtp->debug = true; //是否開啟調試,只在測試程序時使用,正式使用時請將此行注釋
$to = "[email protected]"; //收件人
$subject = "你好";
$body = "<h1>這是一個用 <font color='red'><b> php socket </b></font> 發郵件的測試。
支持SMTP認證!</h1>
";
$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
if($send==1){
echo "郵件發送成功";
}else{
echo "郵件發送失敗<br>";
//echo "原因:".$this->smtp->logs;
}
?>