本款郵件發送功能我們是用了國外一個開源碼的郵件類,大家都可能用過的PHPMailer郵件類很簡單,今天來講一下簡單的使用教程,有需要的朋友可以參考下,同時像其它的附件什么的,各位朋友可以給我意見.
要注意的內容:
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
- /*******************************
- * 作者:李英江
- * 日期:2013-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.49028c.com">http://www.49028c.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@Vevb.com', '歡迎來到Vevb.com!', 'NULL', 'cgsir.com', 'username');
- ?>
新聞熱點
疑難解答