PHPMailer郵件類利用smtp.163.com發送郵件方法

來源:互聯網
上載者:User

第一步:需要下載PHPMailer檔案包phpmailer-1.73.tar.gz 來自開源社區: http://phpmailer.sourceforge.net/
第二步:確認你的伺服器系統已經支援socket 如,通過phpinfo();查看是否支援sockets
  如果沒有這一項就請注意: socket 是屬於PHP擴充部分,編譯時間必須給定一個用於./configure --enable-sockets 的配置選項。

第三步:把檔案解壓到你的web伺服器目錄下,調用類就可以了,說明:首先包含 class.phpmailer.php,然後建立對象,設定參數,調用成員函數。具體請見下面的範例程式碼: 複製代碼 代碼如下:<?php
/*******************************
* 作者:李英江
* 日期:2006-12-7
*******************************/
require("phpmailer/class.phpmailer.php");

function smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name) {
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "200.162.244.66"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourmail"; // SMTP username 注意:普通郵件認證不需要加 @網域名稱
$mail->Password = "mailPassword"; // SMTP password

$mail->From = "yourmail@cgsir.com"; // 寄件者郵箱
$mail->FromName = "cgsir.com管理員"; // 寄件者

$mail->CharSet = "GB2312"; // 這裡指定字元集!
$mail->Encoding = "base64";

$mail->AddAddress($sendto_email,"username"); // 收件者郵箱和姓名
$mail->AddReplyTo("yourmail@cgsir.com","cgsir.com");

//$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true); // send as HTML
// 郵件主題
$mail->Subject = $subject;
// 郵件內容
$mail->Body = '
<html><head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"></head>
<body>
歡迎來到<a href="http://www.cgsir.com">http://www.cgsir.com</a> <br /><br />
感謝您註冊為本站會員!<br /><br />
</body>
</html>
';

$mail->AltBody ="text/html";
if(!$mail->Send())
{
echo "郵件發送有誤 <p>";
echo "郵件錯誤資訊: " . $mail->ErrorInfo;
exit;
}
else {
echo "$user_name 郵件發送成功!<br />";
}
}

// 參數說明(發送到, 郵件主題, 郵件內容, 附加資訊, 使用者名稱)
smtp_mail('yourmail@cgsir.com', '歡迎來到cgsir.com!', 'NULL', 'cgsir.com', 'username');

?>

要注意的內容:
  1. 郵件的字元集設定, $mail->CharSet = "GB2312"; // 這裡指定字元集!在這裡我只指定為GB2312因為這樣Outlook能正常顯示郵件主題,我嘗試過設為utf-8,但在Outlook下顯示亂碼。

  2. 如果是發送html格式的郵件,那麼記得也指定為<meta http-equiv="Content-Type" content="text/html; charset=GB2312">

  3. 如果你想用它來群發郵件的話,記得修改包含檔案函數,如:
  require("phpmailer/class.phpmailer.php");
  改為
  require_once("phpmailer/class.phpmailer.php");
  否則的話會產生類的重定義。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.