分享

让Hostmonster的网站程序也能发送邮件

 开启美好每一天 2015-11-12

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,可以得到下面配置信息:

1Manual Settings
2 
3Mail Server Username: nowamagic+nowamagic.net
4Incoming Mail Server: mail.nowamagic.net
5Incoming Mail Server: (SSL) host.hostmonster.com
6Outgoing Mail Server: mail.nowamagic.net (server requires authentication) port 26
7Outgoing Mail Server: (SSL) host.hostmonster.com  (server requires authentication) port 465
8Supported Incoming Mail Protocols: POP3, POP3S (SSL/TLS), IMAP, IMAPS (SSL/TLS)
9Supported Outgoing Mail Protocols: SMTP, SMTPS (SSL/TLS)

提示:smtp服务器是 mail.yourdomain.com,端口是26,账号是你的邮箱的完整地址 xxxx@xxx.com,密码就是你的邮箱密码。按照提示设置好,就可以使用hostmonster主机提供的SMTP服务了。

示例程序如下,程序使用了PHPmailer库:

01function mailto($nickname, $address)
02{
03    $this->load->helper('url');
04    date_default_timezone_set('PRC');
05    include_once("application/controllers/class.phpmailer.php");
06     
07    $mail = new PHPMailer(); // defaults to using php "mail()"
08     
09    $mail->IsSMTP(); // telling the class to use SMTP
10    $mail->IsHTML(true);
11    $mail->Host       = "mail.nowamagic.net"; // SMTP server
12    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
13                                               // 1 = errors and messages
14                                               // 2 = messages only
15    $mail->SMTPAuth   = true;                  // enable SMTP authentication
16    $mail->Host       = "mail.nowamagic.net"; // sets the SMTP server
17    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
18    $mail->Username   = "nowamagic@nowamagic.net"; // SMTP account username
19    $mail->Password   = "********";        // SMTP account password
20     
21    $mail->CharSet="utf-8";
22     
23    //$body = file_get_contents('application/views/nmra/register.html');
24    //$body = preg_replace('/\\\\/','', $body); //Strip backslashes
25    $body = '<body style="margin: 10px;">';
26    $body .= '<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">';
27    //$body .= '<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div>';
28    $body .= '<p>'.$nickname.',您好。</p>';
29    $body .= '请点击以下链接验证您的邮箱,请注意域名为nowamagic.net:<a href="'.base_url().'accounts/activation/" target="_blank">'.base_url().'accounts/activation/</a>';
30    $body .= '<p>顺祝工作学习愉快,生活舒心。</p>';
31    $body .= '</div></body>';
32    //echo $body;
33 
34    $mail->AddReplyTo("nowamagic@163.com","Gonn");
35    $mail->SetFrom('nowamagic@163.com', 'Gonn');
36    $mail->AddReplyTo("nowamagic@163.com","Gonn");
37    $mail->AddAddress($address, $nickname);
38     
39    $subject = "收到来自简明现代魔法的邮件";
40    $mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
41    // optional, comment out and test
42    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
43    $mail->MsgHTML($body);
44     
45    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
46    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
47     
48    if(!$mail->Send()) {
49        //echo "Mailer Error: " . $mail->ErrorInfo;
50    }
51    else {
52        //echo "Message sent!";
53    }
54}

OK,现在网站程序就能发邮件了。但是并非所有邮箱都能收到,这里测试的话,163,gmail等都能正常收到,而qq则收不到。如果你有更好的方法,请告知我,感谢。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多