php用smtp發送郵件
1.其實用smtp協議發送郵件很簡單,用框架或者原生都可以,我們需要用到class.phpmailer.PHP 和class.smtp.php,大家可以去網上下載。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<form action="mail_send.php" method="post">
<p>收件人:<input type="text" name="" /></p>
<p>標 題:<input type="text" name="title" /></p>
<p>內 容:<textarea name="content" cols="50" rows="5"></textarea></p>
<p>發件人:<input type="text" name="fromman" /></p>
<p><input type="submit" value="發送" /></p>
</form>
</body>
</html>
2、這裏我們封裝好了一個類(Mail.class.php)
<?php
header("content-type:text/html;charset=utf-8");
//引入原來的類文件
require 'class.phpmailer.php';
class Mail {
static public $error = '';
static public function send($title,$content,$user,$address){
$mail= new PHPMailer();
/*服務器相關信息*/
$mail->IsSMTP(); //設置使用SMTP服務器發送
$mail->SMTPAuth = true; //開啓SMTP認證
$mail->Host = 'smtp.163.com'; //設置 SMTP 服務器,自己註冊郵箱服務器地址 QQ則是ssl://smtp.qq.com
$mail->Username = 'zzy9i7'; //發信人的郵箱名稱,本人網易郵箱 [email protected] 這裏就寫
$mail->Password = '******'; //發信人的郵箱密碼
/*內容信息*/
$mail->IsHTML(true); //指定郵件格式爲:html *不加true默認爲以text的方式進行解析
$mail->CharSet ="UTF-8"; //編碼
$mail->From = '[email protected]'; //發件人完整的郵箱名稱
$mail->FromName = $user; //發信人署名
$mail->Subject = $title; //信的標題
$mail->MsgHTML($content); //發信主體內容
//$mail->AddAttachment("15.jpg"); //附件
/*發送郵件*/
$mail->AddAddress($address); //收件人地址
//使用send函數進行發送
if($mail->Send()) {
return true;
} else {
self::$error=$mail->ErrorInfo;
return false;
}
}
}
?>
3、我們要發送郵件就需要引入這個類,然後在做一些簡單的提示
<?php
// 接收值
isset($_POST['address'])?$address=$_POST['address']:$address='';
isset($_POST['titles'])?$titles=$_POST['titles']:$titles='';
isset($_POST['content'])?$content=$_POST['content']:$content='';
isset($_POST['user'])?$user=$_POST['user']:$user='';
//引入類
require 'Mail.class.php';
if( Mail::send($titles,$contents,$user,$address)){
echo "發送成功";
}else{
echo "發送失敗".'<br>';
echo Mail::$error;
}
?>