HostMonster網站程序一般無法發送郵件,因為端口25阻塞了。
許多ISP屏蔽了端口25的使用,而該端口是用來發送郵件的。他們這樣做是為了減少垃圾郵件的發送量。所有通過Internet發送的 e-mail 都要通過端口25, 該通道用來進行e-mail 客戶端和 e-mail服務器之間的通信。
雖然端口25屏蔽很可能成為一個工業標准,但是過濾器會給 e-mail服務器帶來麻煩,導致正常的郵件被當成垃圾郵件處理。
端口25屏蔽可以幫助網絡服務供應商們阻止垃圾信息在他們的網絡上的傳播,但是這樣的話,會給那些有需求使用e-mail服務器發送郵件的人帶來麻煩,這些服務器不僅僅是他們自己的ISP提供的。
屏蔽端口25的網絡服務供應商要求使用他們的SMTP服務器,而不是遠程SMTP服務器或自己電腦上運行的SMTP服務器。
還好的是,HostMonster開放了26端口給SMTP服務器。
我們先到 CPanel -> Email Accounts,創建一個郵件賬戶。
然後到 CPanel -> webmail -> Configure Mail Client,可以得到下面配置信息:
Manual Settings Mail Server Username: bkjia+bkjia.com Incoming Mail Server: mail.bkjia.com Incoming Mail Server: (SSL) host.hostmonster.com Outgoing Mail Server: mail.bkjia.com (server requires authentication) port 26 Outgoing Mail Server: (SSL) host.hostmonster.com (server requires authentication) port 465 Supported Incoming Mail Protocols: POP3, POP3S (SSL/TLS), IMAP, IMAPS (SSL/TLS) Supported Outgoing Mail Protocols: SMTP, SMTPS (SSL/TLS)
提示:smtp服務器是 mail.yourdomain.com,端口是26,帳號是你的郵箱的完整地址 xxxx@xxx.com,密碼就是你的郵箱密碼。按照提示設置好,就可以使用hostmonster主機提供的SMTP服務了。
示例程序如下,程序使用了PHPmailer庫:
function mailto($nickname, $address)
{
$this->load->helper('url');
date_default_timezone_set('PRC');
include_once("application/controllers/class.phpmailer.php");
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSMTP(); // telling the class to use SMTP
$mail->IsHTML(true);
$mail->Host = "mail.bkjia.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.bkjia.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "bkjia@bkjia.com"; // SMTP account username
$mail->Password = "********"; // SMTP account password
$mail->CharSet="utf-8";
//$body = file_get_contents('application/views/nmra/register.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$body = '<body >';
$body .= '<div >';
//$body .= '<div align="center"><img src="images/phpmailer.gif" ></div>';
$body .= '<p>'.$nickname.',您好。</p>';
$body .= '請點擊以下鏈接驗證您的郵箱,請注意域名為bkjia.com:<a href="'.base_url().'accounts/activation/" target="_blank">'.base_url().'accounts/activation/</a>';
$body .= '<p>順祝工作學習愉快,生活舒心。</p>';
$body .= '</div></body>';
//echo $body;
$mail->AddReplyTo("bkjia@163.com","Gonn");
$mail->SetFrom('bkjia@163.com', 'Gonn');
$mail->AddReplyTo("bkjia@163.com","Gonn");
$mail->AddAddress($address, $nickname);
$subject = "收到來自幫客之家的郵件";
$mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
// optional, comment out and test
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
//echo "Message sent!";
}
}
OK,現在網站程序就能發郵件了。但是並非所有郵箱都能收到,這裡測試的話,163,gmail等都能正常收到,而qq則收不到。如果你有更好的方法,請告知我,感謝。