phpmailer【PHP郵件】的用法

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   使用   ar   strong   

第一,需要下載PHPMailer檔案包phpmailer. http://phpmailer.sourceforge.net/
第二,確認你的伺服器系統已經支援socket ,通過phpinfo();查看是否支援sockets(socket 是屬於PHP擴充部分),如果顯現為“enabled”,那就是支援了。
第三,把檔案解壓到你的web伺服器目錄下,調用類就可以了.
首先包含class.phpmailer.php,然後建立對象,設定參數,調用成員函數。具體請見下面的範例程式碼:
執行個體1,做成函數方便調用

 

    <?php        require("phpmailer/class.phpmailer.php");        function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){            $mail = new PHPMailer();            $mail->IsSMTP();                  // send via SMTP  設定(send)使用  SMTP            $mail->Host = "200.162.244.66";   // SMTP servers  $mail->Host = "smtp1.example.com;smpt2.example.com";   指定主伺服器和輔伺服器        $mail->SMTPAuth = true;           // turn on SMTP authentication   開啟 SMTP 認證        $mail->Username = "yourmail";     // SMTP username  注意:普通郵件認證不需要加 @網域名稱            $mail->Password = "mailPassword"; // SMTP password            $mail->From = "[email protected]";      // 寄件者郵箱            $mail->FromName =  "管理員";  // 寄件者                  $mail->CharSet = "GB2312";   // 這裡指定字元集!            $mail->Encoding = "base64";            $mail->AddAddress($sendto_email,"username");  // 收件者郵箱和姓名            $mail->AddReplyTo("[email protected]","yourdomain.com");            $mail->WordWrap = 50; // set word wrap 換行字數            $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件            $mail->AddAttachment("/tmp/image.jpg", "new.jpg");     // attachment 附件         $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>       I love php。       </body>       </html>       ";                                                                                  $mail->AltBody ="text/html";            if(!$mail->Send())            {                echo "郵件發送有誤 <p>";                echo "郵件錯誤資訊: " . $mail->ErrorInfo;                exit;            }            else {                echo "$user_name 郵件發送成功!<br />";            }        }        // 參數說明(發送到, 郵件主題, 郵件內容, 附加資訊, 使用者名稱)        smtp_mail("[email protected]", "歡迎使用phpmailer!", "NULL", "yourdomain.com", "username");        ?>  

 要注意的內容:


  1. 郵件的字元集設定, $mail->CharSet = "GB2312";            // 這裡指定字元集!在這裡我只指定為GB2312因為這樣Outlook能正常顯示郵件主題,我嘗試過設為utf-8但在Outlook下顯示亂碼。
  2. 如果是發送html格式的郵件,那麼記得也指定<meta ... charset=GB2312">
  3. 如果你想用它來群發郵件的話,記得修改包含檔案函數,如:
  require("phpmailer/class.phpmailer.php");
  改為
  require_once("phpmailer/class.phpmailer.php");
  否則的話會產生類的重定義。


以上代碼是引用

經過下午的使用,個人認為要使用phpmailer,
首先需要有一個郵件伺服器
PHP的 mail函數沒有指定,應該是使用的PHP設定的SMTP

而在這裡需要具體指定
同時需要指定郵件伺服器的管理者和密碼

 

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.