在PHPMailer中使用preSend()和getSentMIMEMessage()而不是send()

时间:2017-04-17 23:50:28

标签: php phpmailer

我试图在没有我的开发人员缺席和离开的情况下进行调整。我试图克服GoDaddy的SMTP限制问题。 Synchro已经提供了一个答案,但我担心我需要一起走动才能进行调整。

function sendMail($to, $subject, $message, $from) {
	// Load up the site settings
	global $settings;

	// If the SMTP emails option is enabled in the Admin Panel
	if($settings['smtp_email']) {
		require_once(__DIR__ .'/phpmailer/PHPMailerAutoload.php');

		//Create a new PHPMailer instance
		$mail = new PHPMailer;
		//Tell PHPMailer to use SMTP
		$mail->isSMTP();
		//Enable SMTP debugging
		// 0 = off (for production use)
		// 1 = client messages
		// 2 = client and server messages
		$mail->SMTPDebug = 0;
		//Set the CharSet encoding
		$mail->CharSet = 'UTF-8';
		//Ask for HTML-friendly debug output
		$mail->Debugoutput = 'html';
		//Set the hostname of the mail server
		$mail->Host = $settings['smtp_host'];
		//Set the SMTP port number - likely to be 25, 465 or 587
		$mail->Port = $settings['smtp_port'];
		//Whether to use SMTP authentication
		$mail->SMTPAuth = $settings['smtp_auth'] ? true : false;
		//Username to use for SMTP authentication
		$mail->Username = $settings['smtp_username'];
		//Password to use for SMTP authentication
		$mail->Password = $settings['smtp_password'];
		//Set who the message is to be sent from
		$mail->setFrom($from, $settings['title']);
		//Set an alternative reply-to address
		$mail->addReplyTo($from, $settings['title']);
		//Set who the message is to be sent to
		if(is_array($to)) {
			foreach($to as $address) {
				$mail->addAddress($address);
			}
		} else {
			$mail->addAddress($to);
		}
		//Set the subject line
		$mail->Subject = $subject;
		//Read an HTML message body from an external file, convert referenced images to embedded,
		//convert HTML into a basic plain-text alternative body
		$mail->msgHTML($message);

		//send the message, check for errors
		if(!$mail->send()) {
			// Return the error in the Browser's console
			//echo $mail->ErrorInfo;
		}
	} else {
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
		$headers .= 'From: '.$from.'' . "\r\n" .
			'Reply-To: '.$from . "\r\n" .
			'X-Mailer: PHP/' . phpversion();
		if(is_array($to)) {
			foreach($to as $address) {
				@mail($address, $subject, $message, $headers);
			}
		} else {
			@mail($to, $subject, $message, $headers);
		}
	}
}

问题: - 如何用sendSend()和getSentMIMEMessage()代替send()?

提前感谢协助这位新手......

1 个答案:

答案 0 :(得分:0)

而不是:

//send the message, check for errors
if (!$mail->send()) {
    // Return the error in the Browser's console
    echo $mail->ErrorInfo;
}

这样做:

//send the message, check for errors
if (!$mail->preSend()) {
    // Return the error in the Browser's console
    echo $mail->ErrorInfo;
} else {
    $message = $mail->getSentMIMEMessage();
}

preSend()仍然会对消息结构和地址有效性进行大部分检查,但实际上并没有发送它。 getSentMIMEMessage()获取完整的RFC822消息,您可以通过其他方式发送 - 例如通过发布到基于HTTP的电子邮件服务API,或直接使用PHP的mail()函数。