E. 使用phpmaill發送郵件的例子
阿新 • • 發佈:2017-12-25
tom http email code index.php cut mil gpo utf-8
首先,要下載php_mail軟件包
核心代碼:
index.php
<?php include "mail.php"; if (!empty($_POST[‘to‘]) && !empty($_POST[‘fromname‘]) && !empty($_POST[‘title‘]) && !empty($_POST[‘content‘])) { send_mail($_POST[‘to‘],$_POST[‘fromname‘],$_POST[‘title‘],$_POST[‘content‘]); } ?> <form action="#" method="post"> 接收人:<input type="text" name="to" /><br> 發件人昵稱:<input type="text" name="fromname" /><br> 標題:<input type="text" name="title" /><br> 內容:<input type="text" name="content" style="width:400;height:100;" /><br> <input type="submit" value="提交" /> 註:默認是用作者的QQ郵箱發送 請註意改成自己的數據</form>
mail.php
1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 ini_set("magic_quotes_runtime",0); 4 require ‘php_mail/class.phpmailer.php‘; 5 require ‘php_mail/class.smtp.php‘; 6 7 8 function send_mail($to,$fromname,$title,$content){ 9 try { 10 $mail = new PHPMailer(true); 11 $mail->IsSMTP(); 12 $mail->CharSet=‘UTF-8‘; //設置郵件的字符編碼,這很重要,不然中文亂碼 13 $mail->SMTPAuth = true; //開啟認證 14 $mail->Port = 25; //端口請保持默認 15 $mail->Host = "smtp.qq.com"; //使用QQ郵箱發送 16 $mail->Username = "[email protected]"; //這個可以替換成自己的郵箱 17 $mail->Password = "nelfobtugzckbdae"; //註意 這裏是寫smtp的授權碼 寫的不是QQ密碼,此授權碼不可用 18 //$mail->IsSendmail(); //如果沒有sendmail組件就註釋掉,否則出現“Could not execute: /var/qmail/bin/sendmail ”的錯誤提示 19 $mail->AddReplyTo("[email protected]","mckee");//回復地址 20 $mail->From = "[email protected]"; 21 $mail->FromName = $fromname; 22 $to = $to; 23 $mail->AddAddress($to); 24 $mail->Subject = $title; 25 $mail->Body = $content; 26 $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //當郵件不支持html時備用顯示,可以省略 27 $mail->WordWrap = 80; // 設置每行字符串的長度 28 //$mail->AddAttachment("f:/test.png"); //可以添加附件 29 $mail->IsHTML(true); 30 $mail->Send(); 31 echo ‘郵件已發送‘; 32 33 } catch (phpmailerException $e) { 34 echo "郵件發送失敗:".$e->errorMessage(); 35 } 36 37 38 return true; 39 } 40 //環境 PHP5.3親測可用 41 ?>
效果:
E. 使用phpmaill發送郵件的例子