php使用Mailer實現發送郵件

來源:互聯網
上載者:User
通過php寄送電子郵件,雖然php提供了mail()函數,但不夠靈活,並不好用,而PHPMailer是一個不錯的郵件發送工具,使用起來也是非常簡單!開發環境為centos6.5+php具體步驟如下:

一、下載 class.phpmailer.php

下載地址為:http://www.phpddt.com/usr/uploads/2012/11/3610674900.rar

二、寄件者郵箱開啟smtp

寄件者的郵箱要開啟smtp服務,不同的郵箱開啟方式有所區別,可在郵箱設定進行開啟,具體參見郵箱的協助。

三、建立mailer.php,代碼如下:

error_reporting(E_ALL || ~E_NOTICE);
header("content-type:text/html;charset=utf-8");
include 'class.phpmailer.php';
try {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet='UTF-8'; //設定字元集
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = "smtp.126.com"; //指定smtp伺服器
$mail->Username = "your username"; //你的smtp帳號
$mail->Password = "your password"; //你的smtp密碼
$mail->IsSendmail();
$mail->From = "example@example.com"; //自訂發送郵箱,接收者顯示的發件郵箱
$mail->FromName = "FromName"; //自訂發送人,接收者顯示的寄件者
$to = "example@example.com"; //要發送的郵箱地址
$mail->AddAddress($to);
$mail->Subject = "郵件主題";
$mail->Body = "郵件內容";
$mail->IsHTML(false); //是否設定為html 可自己修改
$mail->Send();
echo 'ok'."\n";
} catch (phpmailerException $e) {
echo "failed".$e->errorMessage();
}
?>

四、修改主機名稱

值得注意的是,發送郵件的主機要更改主機名稱,否則發送速度會非常慢。測試時用了雲主機的網域名稱,可

以實現發送,其他方式均不成功。

五、測試

[root@xxxx testmail]# php mailer.php
ok

以上就介紹了 php使用Mailer實現發送郵件,包括了方面的內容,希望對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.