2014/10/30

phpmailer 發信程式範例(smtp 不用認證)


PHPmailer 是個很好用的發信工具, https://github.com/PHPMailer/PHPMailer (原始碼) 目前在 linux 的套件也都有收錄這套。

如果要安裝直接安裝   libphp-phpmailer  就可以(ubuntu , linux mint 都有收錄)
mtchang@mt ~ $ sudo apt-cache search phpMailer
libphp-phpmailer - full featured email transfer class for PHP
mtchang@mt ~ $ sudo apt-get install libphp-phpmailer

安裝好後他會放在 /usr/share/php/libphp-phpmailer 目錄內
如果程式中需要引用的話,可以透過
require_once "/usr/share/php/libphp-phpmailer/class.phpmailer.php";
的方式引用

底下為 phpmailer 引用的程式碼範例,並針對中文部份有使用 mb_internal_encoding('UTF-8'); 及 mb_encode_mimeheader("$name","UTF-8")); 避免中文的亂碼產生。

// 程式開始  ------------------------------------------------------------------------------------------------------
<?php
// phpmailer 發信函式庫
require_once "/usr/share/php/libphp-phpmailer/class.phpmailer.php";

// ------------------------------------------------------------------------------------
// 接收上一個網頁傳來的變數
// ------------------------------------------------------------------------------------
$name =$_POST['name'];
$email =$_POST['email'];
$comments =$_POST['comments'];

// ------------------------------------------------------------------------------------
// php mailer 發信
// ------------------------------------------------------------------------------------
$system_email='root@jangmt.com';
date_default_timezone_set('Asia/Taipei');
mb_internal_encoding('UTF-8');
$mail             = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
// 設定郵件的內文
$body             = eregi_replace("[\]",'',$comments);
// 寄信伺服器,請勿修改位置限制管院內部可以發信
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.jangmt.com"; // SMTP server
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = false;                  // enable SMTP authentication
// 信件的回信欄位
$mail->AddReplyTo($system_email,mb_encode_mimeheader("系統發信請勿回信","UTF-8"));
// 信件的送件人欄位
$mail->SetFrom($system_email, mb_encode_mimeheader("系統發信請勿回信","UTF-8"));
// 主要收件人
$mail->AddAddress($email, mb_encode_mimeheader("$name","UTF-8"));
// 標題
$mail->Subject    = "Message from $name, internet customer";
$mail->MsgHTML($body);
// 回報執行結果
if(!$mail->Send()) {
  echo "Email Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Email Message sent! <br>";
}
// ------------------------------------------------------------------------------------
// 網頁顯示提示訊息
// ------------------------------------------------------------------------------------
echo "<html> \n";
echo "<head> \n";
echo "<title> Message from $name, internet customer</title>
\n";
echo "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;
charset=utf-8\"> \n";
echo "</head> \n\n";
echo "<body> \n";
echo " Thank you for the message, $name.<br> <br> \n";
echo " The information you provide is: <br> \n";
echo " Name: $name <br> \n";
echo " Email: $email <br> \n";
echo " Comments: $comments <br> \n";
echo "</body> </html> \n";


?>
// 程式結束  ------------------------------------------------------------------------------------------------------

底下為搭配上面發信程式的 form 表單,直接拉 CDN 上面的 bootstrap 3.2 來用。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
<title>顧客回饋單</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

  </head>

  <body>
<!-- REF:  http://getbootstrap.com/css/ -->
    <div class="container">
<div class="page-header">
        <h3>顧客回饋單</h3>
        </div>
<div class="col-lg-6">
<p class="text-info"><strong>親愛的顧客,感謝您本次的消費。請留下您寶貴的意見,促使我們能提供更好的服務,謝謝您~</strong></p>
<form method="post" action="sendpost.php">
<p class="text-info">
您的大名:
<input name="name" type="text" />&nbsp;</td>
</p>
<p class="text-info">
您的email:
<input name="email" type="text" />&nbsp;</td>
</p>
<p class="text-info">
您的意見:
<textarea name="comments" cols="20" rows="2"></textarea>
</p>
<input name="Submit1" type="submit" value="submit" />&nbsp;  <input name="Reset1" type="reset" value="reset" />
</form>
</div>

     <div  class="bg-info">
<p></p>
<p>Company 2014 by mtchang </p>
    </div>

    </div> <!-- /container -->

  </body>
</html>
張貼留言