phpmailer 安装或者下载方式:
- 1、从 github 上下载:https://github.com/PHPMailer/PHPMailer/
2、使用 composer 安装:
composer require phpmailer/phpmailer
发送之前需要拥有自己的邮件服务器,测试的时候其实用自己申请的免费邮箱最方便了,不需要自己再搭建服务器了,可能要配置邮箱的SMTP服务,大部分公共邮箱(163、qq等)为了安全默认是关闭的。
3、配置调用
引用文件:use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
使用:
function sendEmail(){ //sendEmail()里面可带参数
//判断openssl是否开启
$openssl_funcs = get_extension_funcs('openssl');
if(!$openssl_funcs){
return ['code'=>0 , 'msg'=>'请先开启openssl扩展'];
}
$config['email_smtp_server'] = '127.0.0.1'; //服务器地址
$config['email_smtp_port'] = 25; //端口
$config['email_smtp_user'] = ''; //邮件发送账号
$config['email_smtp_password'] = ''; //邮件发送密码
$config['email_smtp_name'] = ''; //邮件发送名称
$to = "510435764@qq.com"; //接收邮箱
$subject = '测试标题'; //邮件标题
$content = '测试内容'; //邮件内容
$addAttachment = ''; //附件
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->isSMTP();
$mail->SMTPDebug = 0; //开启错误调试0关闭 1开启
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//调试输出格式
$mail->Debugoutput = 'html';
//smtp服务器
$mail->Host = $config['email_smtp_server'];
//端口 - likely to be 25, 465 or 587
$mail->Port = $config['email_smtp_port'];
//$mail->SMTPSecure = 'ssl';// 使用安全协议 tls,ssl
//用户名
$mail->Username = $config['email_smtp_user'];
//密码
$mail->Password = $config['email_smtp_password'];
//Set who the message is to be sent from
$mail->setFrom($config['email_smtp_user'],$config['email_smtp_name']);
//回复地址
//$mail->addReplyTo('replyto@example.com', 'First Last');
//接收邮件方
if(is_array($to)){
foreach ($to as $v){
$mail->addAddress($v);
}
}else{
$mail->addAddress($to);
}
$mail->isHTML(true);// send as HTML
//标题
$mail->Subject = $subject;
//邮箱正文
$mail->Body = $content;
//添加附件
if (!empty($addAttachment)){
$mail->addAttachment($addAttachment);
}
try {
//$mail->send();
if (!$mail->send()) {// 发送邮件
echo "发送失败: " . $mail->ErrorInfo;// 输出错误信息
} else {
echo '发送成功';
}
}catch (Exception $e){
return ['code'=>0 , 'msg'=>$e->getMessage()];
}
}
3 条评论
怎么收藏这篇文章?
叼茂SEO.bfbikes.com
想想你的文章写的特别好https://www.jiwenlaw.com/