编辑:这个答案可能已经过时了。在我写这篇文章的时候,SwiftMailer 库存在一些问题。在这一点上,一切都可以正常工作,SwiftMailer
并且被认为是更好的库,提供比PHPMailer
.
我建议你使用 phpmailer。它是我用过的最稳定的邮件库之一。这是一个应该可以工作的示例代码:
include("./phpmailer/class.phpmailer.php");
$mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP();
$mail->Host = "YourDomainName.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "YourSMTPMailServer.com";
$mail->Port = 587;
$mail->Username = "your-auth-user@yoursmtpmailsercer.com";
$mail->Password = "password"; // GMAIL password
$mail->AddAddress("sendToThis@email.com", '<< >> ! " Receiver Name');
$mail->SetFrom('sendFROMthis@email.com', '<< >> ! " Sender Name');
$mail->Subject = "A testing subject";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('This is my <b>html</b> testing email, sent '.time());
$mail->Send();
您需要对其进行配置,以便它连接到您的电子邮件服务器,但它应该可以工作。到目前为止,Phpmailer 逃脱了我尝试过的所有事情。我唯一检查的是“sendToThis@email.com”。我用这段代码来做:
$email = "sendToThis@email.com";
$email = filter_var(filter_var($email,FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
if($email){
echo "This email is valid!";
} else {
echo "This email is INVALID!";
}
我希望这有帮助 :)