* 将文件作为Gmail附件备份到Gmail中 * ver 1.0 * 2008-11-15 zGia! * wuliuqiba@gmail.com PHPMailer: http://phpmailer.codeworxtech.com/ http://sourceforge.net/projects/phpmailer
PHP email transport class featuring file attachments, SMTP servers, CCs, BCCs, HTML messages, word wrap, and more. Sends email via sendmail, PHP mail(), QMail, or with SMTP.
php
/**
* 将文件作为Gmail附件备份到Gmail中
* ver 1.0
* 2008-11-15 zGia!
* wuliuqiba@gmail.com
*/
error_reporting(E_ALL);
//error_reporting(E_STRICT);
date_default_timezone_set('Asia/Shanghai');
//@header('Content-Type: text/html; charset=utf-8');
include("class.phpmailer.php");
// ##############################################################################
// Gmail标签
// 通过filter,设置邮件自动分拣。比如设置filter:
// Matches: to:(xxxxx+book@gmail.com)
// Do this: Skip Inbox, Apply label "book"
$labels = array(
"nolabel" = "",
"book" => "book",
"backup" => "backup"
);
// 是否将多个附件发送到一个邮件中
$more_attaches_email = false;
// 附件
// 不能发送可执行文件或大于 20 MB 的邮件
$attaches = array(
"E:\phpmailer.rar",
);
// 发邮件
if ($more_attaches_email)
{
mailer($labels['book'], $attaches);
}
else
{
foreach ($attaches as $attach)
{
mailer($labels['backup'], $attach);
}
}
// ##############################################################################
// 传送门
function mailer($label, &$attaches)
{
unset($mail);
$mail =& init_mail($label);
send_mail($mail, $attaches);
}
// ##############################################################################
// 发邮件
function send_mail(&$mail, &$attaches)
{
// 邮件主题,正文
$subject = "";
if(empty($attaches))
{
echo "No attach!";
exit;
}
// 多附件
if(is_array($attaches))
{
foreach ($attaches as $attach)
{
$mail->AddAttachment($attach);
$subject .= "|" . basename($attach);
}
}
// 单附件
else
{
$mail->AddAttachment($attaches);
$subject .= "|" . basename($attaches);
}
$subject = preg_replace("/^\|/i", "", $subject);
echo "Sending $subject now......\n\t\t";
$mail->Subject = $subject;
$mail->Body = preg_replace("/\|/i", "
", $subject);
if(!$mail->Send()) {
echo "Failure: " . $mail->ErrorInfo;
} else {
echo "Success, message sent.";
}
echo "\n";
}
// ##############################################################################
// 初始化 PHPMailer
function init_mail($label = "")
{
// 构造
$mail = new PHPMailer();
// 使用 SMTP
$mail->IsSMTP();
// 激活 SMTP 验证你哦是
$mail->SMTPAuth = true;
// 下面3个配置不明白的,可以查看此链接:
// http://mail.google.com/support/bin/answer.py?answer=76147
// 此服务器要求安全连接
$mail->SMTPSecure = "ssl";
// 使用 Gmail 作为 SMTP 服务器
$mail->Host = "smtp.gmail.com";
// Gmail 服务器的 SMTP 端口号
$mail->Port = 465;
// ****************** 从这里开始 *************************
// 发送邮件的 Gmail 账户
$mail->Username = "username@gmail.com";
$mail->Password = "password";
// 添加回复到
//$mail->AddReplyTo("replyto@gmail.com","replyto");
// 发信人
$mail->From = "sender@gmail.com";
$mail->FromName = "sender";
// 收信人
$mail->AddAddress("recipients" . ($label ? "+$label" : "") . "@gmail.com", "recipients");
// ****************** 到这里结束,改成你自己的gmail账号 *************************
// 邮件编码
$mail->CharSet = "utf-8";
// HTML 格式邮件,如果使用文本模式查看的话,所显示的信息
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
// 正文折行
$mail->WordWrap = 50;
// 可以使用某个文件内容作为邮件正文
//$body = $mail->getFile('contents.html');
//$body = eregi_replace("[\]",'',$body);
//$mail->MsgHTML($body);
// 发送 HTML 格式邮件
$mail->IsHTML(true);
return $mail;
}