首页 随笔 乐走天涯 程序资料 评论中心 Tag 论坛 其他资源 搜索 联系我 关于 RSS

如何使用 Gmail/Google Apps 的 SMTP 在线发送 Newsletter


日期: 2009-01-22 23:01 | 联系我 | 关注我: Telegram, Twitter

  有不少站长读了 ZAC 的文章《Email Marketing - 电子邮件营销终极手册》之后,准备着手改造自己网站上的 Newsletter 程序。对于发送邮件列表,最安全的办法,是使用第三方公司的邮件列表服务。因为使用自己的域名发送,尽管你发送的是正规内容,用户的订阅过程也采用了二次确认,然而一旦被投诉列入反垃圾邮件组织的黑名单,就比较麻烦了,要折腾许久才会解封。而第三方的专业邮件列表服务公司,一般都跟各大 ISP 互有协议。所以国外许多大公司也都是用第三方的邮件列表服务。

  但大部分站长还是希望使用自己的域名来发送。主要原因不外乎是:

  • 第三方服务需要收费。
  • 可控性强。自己拥有 100% 的控制权。
  • 使用自己的域名,让用户看起来,觉得那么“专业”一点。

PHPMailer: Email Transfer Class for PHP

PHPMailer: Email Transfer Class for PHP

  现在,将自己网站的邮件服务交给 Google Apps 托管的站长越来越多。Google Apps 以及 Gmail 的 SMTP 服务器,要求使用 SSL 连接,但 PHP 自带的 mail 函数以及大多数 PHP 邮件程序目前还不能支持 SSL。那么,如何使用 Gmail 或者 Google Apps 的 SMTP 服务器在线发送邮件列表呢?我的解决方案是使用 PHPMailer

  PHPMailer 是基于 GNU/LGPL 的 PHP 开源邮件发送程序,以 PHP Class 的方式提供给 PHP 开发者调用。由 codeworxtech 开发。目前提供 PHP4 和 PHP5 两个版本免费下载。主要特性:

  • Supports emails digitally signed with S/MIME encryption!
  • Supports emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Works on any platform.
  • Supports Text & HTML emails.
  • Embedded image support.
  • Multipart/alternative emails for mail clients that do not read HTML email.
  • Flexible debugging.
  • Custom mail headers.
  • Redundant SMTP servers.
  • Support for 8bit, base64, binary, and quoted-printable encoding.
  • Word wrap.
  • Multiple fs, string, and binary attachments (those from database, string, etc).
  • SMTP authentication.
  • Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.

PHP 示例代码: 使用 Gmail 或 Google Apps 的 SMTP Server 在线发送邮件

  通过 Gmail/Google Apps 的 SMTP 服务器发送邮件,示例代码如下:

<?php
// example on using PHPMailer with GMAIL

include("class.phpmailer.php");

$mail             = new PHPMailer();

$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port

$mail->Username   = "yourname@gmail.com";  // GMAIL username
$mail->Password   = "password";            // GMAIL password

$mail->From       = "replyto@yourdomain.com";
$mail->FromName   = "Webmaster";
$mail->Subject    = "This is the subject";
$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddReplyTo("replyto@yourdomain.com","Webmaster");

$mail->AddAttachment("/path/to/file.zip");             // attachment
$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment

$mail->AddAddress("username@domain.com","First Last");

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) 
  echo "Mailer Error: " . $mail->ErrorInfo;
else
  echo "Message has been sent";
?>

  可以看出 PHPMailer 的使用很简单。PHP 程序员可以很容易参照上述代码使用 PHPMailer 改造自己的 Newsletter 程序

  目前,使用 Gmail/Google Apps 的 SMTP Mail Server 发送邮件的每日上限是 500封。这点需要注意。

有关邮件服务器、Newsletter的其他资源


 文章评论

第 1 楼  发表于 2009-01-23 16:40 | xjb 的所有评论
赶明我也做个Newsletter

第 2 楼  发表于 2009-01-23 16:55 | 待业书生 的所有评论
就保证在每天500封以内是不是也有被封的危险? 内容方面要写好privacy policy吧.还有什么注意事项吗?怕被连GMAIL帐号都封了.
回复于 2009-03-30 13:13:
每天在 500 封以内的话,收到的退信不能太多。否则帐号会被临时停用一天。privacy policy 之类的是肯定要注明的。

第 3 楼  发表于 2009-04-24 18:41 | Antonio 的所有评论
小辉你好,我下了PHPMailer ,用你上面的例子发送邮件,当使用国内的163或126时能正常收发邮件,而改用gmail时为什么会出现以下错误?

SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
回复于 2009-04-24 22:24:
GMAIL 要用 SSL 连接。你要看你的主机是否装了SSL并打开了 SSL 端口(一般是465)

第 4 楼  发表于 2010-08-28 10:21 | 我途求职 的所有评论
我用本机SMTP发给gmail不知为什么收不到,其他邮箱都能收到

共有评论 4 条, 显示 4 条。

发表你的评论
如果你想针对此文发表评论, 请填写下列表单:
姓名: * 必填 (Twitter 用户可输入以 @ 开头的用户名, Steemit 用户可输入 @@ 开头的用户名)
E-mail: 可选 (不会被公开。如果我回复了你的评论,你将会收到邮件通知)
反垃圾广告: 为了防止广告机器人自动发贴, 请计算下列表达式的值:
9 x 2 + 2 = * 必填
评论内容:
* 必填
你可以使用下列标签修饰文字:
[b] 文字 [/b]: 加粗文字
[quote] 文字 [/quote]: 引用文字

 
首页 随笔 乐走天涯 猎户星 Google Earth 程序资料 程序生活 评论 Tag 论坛 资源 搜索 联系 关于 隐私声明 版权声明 订阅邮件

程序员小辉 建站于 1997 ◇ 做一名最好的开发者是我不变的理想。
Copyright © XiaoHui.com; 保留所有权利。