使用phpmaill發送郵件的例子

首先,要下載php_mail軟件包php

核心代碼:html

index.phppost

<?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.phpui

 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 = "903215283@qq.com"; //這個能夠替換成本身的郵箱
17 $mail->Password = "nelfobtugzckbdae"; //注意 這裏是寫smtp的受權碼 寫的不是QQ密碼,此受權碼不可用
18 //$mail->IsSendmail(); //若是沒有sendmail組件就註釋掉,不然出現「Could not execute: /var/qmail/bin/sendmail 」的錯誤提示 
19 $mail->AddReplyTo("903215283@qq.com","mckee");//回覆地址 
20 $mail->From = "903215283@qq.com"; 
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 ?> 

效果:編碼

相關文章
相關標籤/搜索