首頁 新聞動態 如何用php結合phpmailer發送郵件

如何用php結合phpmailer發送郵件

來源: 網企建站網站建設 | 時間:2020/10/30 | 瀏覽:12532

php本身提供了一個郵件發送函數mail,可以在程序中直接發送電子郵件,不過該函數要求服務器支持sendmail或者必須設置一臺不需要中繼的郵件發送服務器,但現在要找到一臺不需要身份驗證的郵件發送中繼幾乎不可能。

所以使用mail函數往往無法成功發送email。

自己如果熟悉SMTP協議,結合socket功能就可以編寫高效穩定的郵件發送程序,但對一般用戶來說太困難.好在互聯網上已經有很多別人編寫好的郵件發送模塊,我們只需要下載後簡單調用即可,十分方便。

在此我們給用戶推薦一個php下功能十分強大,易用且免費的SMTP類模塊-PHPMailer,(下載地址http://downinfo.myhostadmin.net/mail/PHPMailer-BMH_v5.0.0.zip)PHPMailer是一個用於發送電子郵件的PHP函數包。它提供的功能包括:

*.在發送郵時指定多個收件人,抄送地址,暗送地址和回覆地址
*.支持多種郵件編碼包括:8bit,base64,binary和quoted-printable
*.支持SMTP驗證
*.支持冗餘SMTP服務器
*.支持帶附件的郵件和Html格式的郵件
*.自定義郵件頭
*.支持在郵件中嵌入圖片
*.調試靈活
*.經測試兼容的SMTP服務器包括:Sendmail,Postfix,Imail,Exchange等
*.可運行在任何平臺之上

下載該組件後,按如下示例編寫代碼即可實現php在線發送郵件.
 

一:前臺表單


<html>
<body>
<h3>phpmailer Unit Test</h3>
請你輸入<font color="#FF6666">收信</font>的郵箱地址:
<form name="phpmailer" action="send.php" method="post">
<input type="hidden" name="submitted" value="1"/>
郵箱地址: <input type="text" size="50" name="address" />
<br/>
<input type="submit" value="發送"/>
</form>
</body>
</html>


二:後臺PHP程序


 <?php
require("class.phpmailer.php"); //下載的文件必須放在該文件所在目錄
$mail = new PHPMailer(); //建立郵件發送類
$address = $_POST['address'];
$mail->IsSMTP(); // 使用SMTP方式發送
$mail->Host = "mail.xxxxx.com"; // 您的企業郵局域名
$mail->SMTPAuth = true; // 啓用SMTP驗證功能
$mail->Username = "user@xxxx.com"; // 郵局用戶名(請填寫完整的email地址)
$mail->Password = "******"; // 郵局密碼

$mail->From = "user@xxxx.com"; //郵件發送者email地址
$mail->FromName = "您的名稱";
$mail->AddAddress("$address", "");//收件人地址,可以替換成任何想要接收郵件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");

//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

$mail->Subject = "PHPMailer測試郵件"; //郵件標題
$mail->Body = "Hello,這是測試郵件"; //郵件內容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略

if(!$mail->Send())
{
 echo "郵件發送失敗. <p>";
 echo "錯誤原因: " . $mail->ErrorInfo;
 exit;
}

echo "郵件發送成功";
?>


TAG:香港外貿郵箱
相關案例
台灣-巨懋企業有限公司 BENNIC 於1978年由創始人范明達先生在台北所創立. 1979年設立台灣廠. 1997年設立中國惠州廠. 2019年設立泰國廠. 企業專注於無極電容的生產與銷售. 2003 開始生產電感器與電阻器並進行分音器的組裝, 以服務客戶. 40多年來, 產品已運用於世界各大車廠與名牌音響.
Shenzhen Foomee Intelligent Technology Co., Ltd FOOMEE is an international brand specializing in mobile accessories and consumer electronics. We are committed to delivering products that combine quality, design, and competitiveness for global markets. With strong capabilities in product
SHENZHEN KANGHAO INDUSTRIAL CO.,LTD. SHENZHEN KANGHAO INDUSTRIAL CO.,LTD is a professional plastic products manufacturing factory based in Shenzhen, Guangdong, China. Founded in 2008, we specialize in eco-friendly eco friendly and sustainable products, various plastic parts, and plastic mold R&D & manufacturing — delivering full-cycle solutions from mold design to mass production.
Shenzhen Ruixin Zhilian Technology Co., Ltd. Shenzhen Ruixin Zhilian Technology Co., Ltd. is a high-tech enterprise specializing in the research, development, design and manufacturing of high-performance motherboards. Since its establishment, we have been committed to providing global customers with stable, efficient and innovative hardware computing platform solutions, and is a trusted core partner in the fields of communications, data centers and intelligent mobility.