其实用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/htmlcharset=UTF-8">
<title>Document</title>
</head>
<body>
<form action="mail_send.php" method="post">
<p>收件人:<input type="text" name="address" /></p>
<p>标 题:<input type="text" name="title" /></p>
<p>内 容:<textarea name="content" cols="50" rows="5"></textarea></p>
<p>发件人:<input type="text" name="user" /></p>
<p><input type="submit" value="发送" /></p>
</form>
</body>
</html>
写了一个mail_send.php然后引入一个封装的类
<?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
}
?>
3、这里是我们封装好了一个类(Mail.class.php)上面引入的类
<?php
header("content-type:text/htmlcharset=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
}
}
}
?>
php发送邮件,php发送电子邮件,使用php发送邮件,php如何发送邮件方法/步骤
1
在实际项目开发中,我们经常需要得到用户的反馈信息并及时回复。普通的留言板有一定的内容限制,而邮件则能满足这个需求。今天给大家演示一下怎么利用PHP发送电子邮件。
2
1.首先我们需要一个用来发送电子邮件的文件的程序,也就是一个php文件,流行的phpmail有很多,今天以smtp.php为例来演示。
3
2.将其复制到你的项目文件内,具体路径根据自己的实际情况,这里建了一个test文件,用来设置邮件参数,大家也可以自定义邮件参数文件。(PS:一般都是通过表单接受的)
4
3.smtp.php文件如图所示,大家不需要管代码,会用就行,有时间了也可以研究研究哈。
5
4.设置邮件参数,具体代码如下:
//引入发送邮件类
require("smtp.php")
//使用163邮箱服务器
$smtpserver = "smtp.163.com"
//163邮箱服务器端口
$smtpserverport = 25
//你的163服务器邮箱账号
$smtpusermail = "@163.com"
//收件人邮箱
$smtpemailto = "@qq.com"
//你的邮箱账号(去掉@163.com)
$smtpuser = ""//SMTP服务器的用户帐号
//你的邮箱密码
$smtppass = "" //SMTP服务器的用户密码
6
5.设置邮件内容,代码如下:
//邮件主题
$mailsubject = "测试邮件发送"
//邮件内容
$mailbody = "PHP+MySQL"
//邮件格式(HTML/TXT),TXT为文本邮件
$mailtype = "TXT"
//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass)
//是否显示发送的调试信息
$smtp->debug = TRUE
//发送邮件
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
刷新页面后邮件发送,参数不出错的话1分钟左右即可发送成功。我在测试的时候没有发送成功提示,自己登录邮箱查看的,如图所示,内容和上面设置的一样。
首先,我不想给phpmailer这个东西做广告,但是我确实使用的这个东西,很费解。
下载一个phpmailer类,里面主要的是三个文件class.phpmailer.php\class.pop3.php\class.smtp.php
将这三个文件放到同一个文件夹.eg:papmailer
申请一个网络邮件服务器(我用的是163服务器)
编写发送邮件代码如下
/*** 发送邮件
* @param unknown_type $sendto_email 接收人Email
* @param unknown_type $subject 邮件主题
* @param unknown_type $body 邮件内容
* @param unknown_type $user_name 接受人姓名
*/
function send_email( $sendto_email, $subject, $body, $user_name){
require_once "phpmailer/class.phpmailer.php"
$mail = new PHPMailer() //创建类对象
$mail->IsSMTP()
$mail->Host = "smtp.163.com" // SMTP servers 使用163服务器,邮件smtp服务器
$mail->SMTPAuth = true // turn on SMTP authentication
$mail->Username = "你申请的163邮箱帐号" // SMTP username 注意:普通邮件认证不需要加 @域名
$mail->Password = "163邮箱密码" // SMTP password
$mail->From = "发件人邮箱,可以使用上面163邮箱" // 发件人邮箱
$mail->FromName = "发件人名称,自己随意命名" // 发件人
$mail->CharSet = "utf8" // 这里指定字符集!
$mail->Encoding = "base64"
$mail->AddAddress($sendto_email,$user_name) // 收件人邮箱和姓名
$mail->IsHTML(true) // send as HTML
// 邮件主题
$mail->Subject = $subject
// 邮件内容
$mail->Body = $body
$mail->AltBody ="text/html"
if(!$mail->Send()){
return $mail->ErrorInfo
}else {
return true
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)