require(ROOT.'/class/phpMailer.class.php')//邮件发送类
/**
* 发送邮件
* @param string $to 接收人邮件地址
* @param string $title 邮件标题
* @param string $contents 邮件内容 支持HTML格式
* @param string $type 判断是否要加附件
* @param string $accessory 附件的名字
* @return 成功返回true,失败返回错误信息
*/
function sendEmail($to,$title,$contents,$type = '',$accessory =''){
$mail = new PhpMailer(true)
$mail->IsSMTP()
$mail->CharSet ="UTF-8"//编码
$mail->Debugoutput = 'html'// 支持HTML格式
$mail->Host = T_SMTP_SERVER//HOST 地址
$mail->Port = 25//端口
$mail->SMTPAuth = true
$mail->Username = T_SMTP_LOGIN//用户名
$mail->Password = T_SMTP_PASSWORD//密码
$mail->SetFrom(T_SMTP_FROM,T_SMTP_FROM_NAME)//发件人地址, 发件人名称
$mail->AddAddress($to)//收信人地址
//$mail->Subject = "=?utf-8?B?" . base64_encode() . "?="
if (!empty($type)) {
$mail->AddAttachment($type,$accessory) // 添加附件,并指定名称
}
$mail->Subject = $title//邮件标题
$mail->MsgHTML($contents)
if ($mail->Send()){
return true
}else{
return $mail->errorMessage()
}
}
<?phprequire_once "class/class.phpmailer.php"
$sendTo=$_POST['sendTo']
$bodyHtml=$_POST['bodyHtml']
$Subject="文件发送通知".date('Y-m-d H:i:s',time())
$SubjectGbk=iconv("utf-8","gbk",$Subject)
$fromer=$_SESSION['user_email']
$emailPsw=$_SESSION['user_emailpsw']
$sender = new mySendMail($sendTo, $fromer, $Subject, $bodyHtml,$fromer, $emailPsw )
echo json_encode($sender->getSendOK())
//自定义邮件发送类
class mySendMail {
private $to =""
private $cc =""
private $Subject =""
private $body =""
private $mail =null
private $sendOK =array('success'=>false,'msg'=>'')
public function __construct($to='',$cc="",$Subject="",$body="",$fromer="", $emailPsw="") {
if ($to==""){
$this->sendOK=array('success'=>false,'msg'=>'没有提供收件人')
return
}
$this->mail=new PHPMailer(true)
$this->mail->IsSMTP()
$this->mail->CharSet='UTF-8' //设置邮件的字符编码,这很重要,不然中文乱码
$this->mail->SMTPAuth = true //开启认证
$this->mail->Port = 25
$this->mail->Host = "mail.126.com"
if ($fromer!=="" ){
$this->mail->Username = $fromer
$this->mail->Password = $emailPsw
$this->mail->AddReplyTo($fromer,"")//回复地址
$this->mail->From = $fromer
$this->mail->FromName = "webserver"
$this->mail->AddCC($fromer,"")
}else{
$this->mail->Username = "Username@126.com"
$this->mail->Password = "password"
$this->mail->AddReplyTo("Username@126.com","Username")//回复地址
$this->mail->From = "Username@126.com"
$this->mail->FromName = "Username@126.com"
}
if ($cc!==""){
$this->mail->AddCC($cc)
}
$toArr=explode('|',$to)
$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"
$ok_addr=0
for ($i=0$i<sizeof($toArr)$i++){
if ( preg_match( $pattern, $toArr[$i]) ){
$this->mail->AddAddress($toArr[$i])
$ok_addr++
}
}
if($ok_addr==0){
$this->sendOK= array('success'=>false,'msg'=>'没有一个收件者的地址是合法的')
}
$this->mail->Subject =$Subject
$this->mail->Body =$body
$this->mail->WordWrap = 80
$this->mail->IsHTML(true)
try{
$this->mail->Send()
$this->sendOK= array('success'=>true,'msg'=>'邮件已发送')
} catch (phpmailerException $e) {
$this->sendOK= array('success'=>false,'msg'=>$e->errorMessage())
}
}
public function getSendOK(){
return $this->sendOK
}
}
class.phpmailer.php 贴不下 自行下载或者私信 邮箱地址
可以采用phpmailer类,来做邮件发送,这也是很多PHP程序所采用的一个类发送
require(ROOT.'/class/phpMailer.class.php')//邮件发送类
/**
*
发送邮件
*
@param
string
$to
接收人邮件地址
*
@param
string
$title
邮件标题
*
@param
string
$contents
邮件内容
支持HTML格式
*
@param
string
$type
判断是否要加附件
*
@param
string
$accessory
附件的名字
*
@return
成功返回true,失败返回错误信息
*/
function
sendEmail($to,$title,$contents,$type
=
'',$accessory
=''){
=
new
PhpMailer(true)
$mail->IsSMTP()
$mail->CharSet
="UTF-8"//编码
$mail->Debugoutput
=
'html'//
支持HTML格式
$mail->Host
=
T_SMTP_SERVER//HOST
地址
$mail->Port
=
25//端口
$mail->SMTPAuth
=
true
$mail->Username
=
T_SMTP_LOGIN//用户名
$mail->Password
=
T_SMTP_PASSWORD//密码
$mail->SetFrom(T_SMTP_FROM,T_SMTP_FROM_NAME)//发件人地址,
发件人名称
$mail->AddAddress($to)//收信人地址
//$mail->Subject
=
"=?utf-8?B?"
.
base64_encode()
.
"?="
if
(!empty($type))
{
$mail->AddAttachment($type,$accessory)
//
添加附件,并指定名称
}
$mail->Subject
=
$title//邮件标题
$mail->MsgHTML($contents)
if
($mail->Send()){
return
true
}else{
return
$mail->errorMessage()
}
}
望采纳
Thx
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)