想发带附件的邮件,使用php语言。

有一个著名的phpmailer的库(https://github.com//PHPMailer//PHPMailer//wiki//Troubleshooting),在它的troubleshooting里面提到了godaddy的“特殊”。

下面记录一下我的发邮件过程。

第一步,先申请cpanel邮箱,打开cpanel,如下图所示

利用godaddy的cpanel邮箱服务器的smtp发邮件

创建了指定域名的邮箱账户。

第二步,参考邮件mx配置指导链接:https://sg.godaddy.com/zh/help/cpanel-dns-8852

打开dns管理页面:https://dcc.godaddy.com/manage/your_domain/dns

添加解析记录:

利用godaddy的cpanel邮箱服务器的smtp发邮件

 第三步,回到cpanel点击邮件配置:

利用godaddy的cpanel邮箱服务器的smtp发邮件

第四步,用户名,密码,发送服务器,端口号,这四个是一会儿发邮件要用到的。

利用godaddy的cpanel邮箱服务器的smtp发邮件

第五步,代码部分:

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!';
	}

就可以了。

 

相关文章:

  • 2021-09-01
  • 2022-01-18
  • 2021-12-19
  • 2022-12-23
  • 2021-12-23
  • 2022-02-07
  • 2021-12-05
猜你喜欢
  • 2022-02-09
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2021-06-08
  • 2021-11-20
相关资源
相似解决方案