想发带附件的邮件,使用php语言。
有一个著名的phpmailer的库(https://github.com//PHPMailer//PHPMailer//wiki//Troubleshooting),在它的troubleshooting里面提到了godaddy的“特殊”。
下面记录一下我的发邮件过程。
第一步,先申请cpanel邮箱,打开cpanel,如下图所示
创建了指定域名的邮箱账户。
第二步,参考邮件mx配置指导链接:https://sg.godaddy.com/zh/help/cpanel-dns-8852
打开dns管理页面:https://dcc.godaddy.com/manage/your_domain/dns
添加解析记录:
第三步,回到cpanel点击邮件配置:
第四步,用户名,密码,发送服务器,端口号,这四个是一会儿发邮件要用到的。
第五步,代码部分:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../lib/PHPMailer/src/Exception.php';
require '../lib/PHPMailer/src/PHPMailer.php';
require '../lib/PHPMailer/src/SMTP.php';
function sendMail($email){
$mail = new PHPMailer;
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
$mail->CharSet ='UTF-8';
$mail->Host = '此处填写发送服务器';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
$mail->FromName = "我的别名";
$mail->From = '此处填写用户名';
$mail->Username = '此处填写用户名';
$mail->Password = '此处填写密码';
$mail->SMTPAuth = true;
$mail->SMTPSecure = true;
//$mail->addReplyTo('[email protected]', 'First Last');
$mail->addAddress($email, '');
$mail->Subject = '过年好';
$mail->IsHtml(true);
$mail->Body = '拜个早年';
$mail->addAttachment('./canglaoshi.jpg');
if (!$mail->send()) {
return false;
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
return true;
echo 'Message sent!';
}
就可以了。