我会建议 SMTP 是要走的路,我发现无论如何在垃圾邮件过滤器等方面我都能得到更好的结果。
我首选的解决方案是使用:https://github.com/PHPMailer/PHPMailer
我已经构建了一个辅助函数来让我更容易实现它
**函数-文件名:smtp.function **
<?php
//SMTP controller FUNCTION for PHPMailer script
//source of script: https://github.com/PHPMailer/PHPMailer
require ('PHPMailer-master/PHPMailerAutoload.php');
function SMTP
($to, $subject,$html, $text='')
{
//UPDATE SETTINGS TO MATCH PROJECT SETTINGS
$settings = array('host'=>'XXXXXX','username'=>'XXXXXX','password'=>'XXXXXX','fromEmail'=>'XXXXXX', 'fromName'=>'XXXXXX','replyEmail'=>'XXXXXX', 'replyName'=>'XXXXXX');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $settings['host']; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $settings['username']; // SMTP username
$mail->Password = $settings['password']; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $settings['fromEmail'];
$mail->FromName = $settings['fromName'];
//$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress($to); // Name is optional
$mail->addReplyTo($settings['replyEmail'], $settings['replyName']);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $text;
//WHAT TO DO IF EMAIL SENDS OR NOT
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
echo 'Message has been sent';
}
}
?>
在 PHP 中调用它
<?php
require('smtp.function');
SMTP('to@domain.com',' This is a test of the SMTP class by Alex','<strong>This test</strong> is <a href="http://www.google.co.uk">HTML CODE</a>');
?>