PHP邮件功能不发送到任何其他电子邮件

时间:2017-11-16 16:06:06

标签: php email html-email

我正在使用PHP内置的邮件功能发送确认电子邮件。但是,它仅在我的服务器上本地工作,该服务器已分配给我的大学。该网站是http://adonnelly759.students.cs.qub.ac.uk/thatch/

当发送联系表格时,它应该通过管理员电子邮件地址向我自己发送一封电子邮件,并确认电子邮件到他们提供的客户电子邮件地址。

我已经设置了如下代码:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <Thatch>' . "\r\n";

// Sending the email to the users provided e-mail
mail($email, $userSub, $userMsg, $headers);

更新:决定放弃mail() function并选择SwiftMailer。简单易用。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

PHP mail()函数不是最好的方法。转到mail-tester.com并复制显示的随机电子邮件地址,现在使用您的代码向此地址发送电子邮件,返回mail-tester.com并查看结果。我怀疑你得分非常慢,这就是为什么你的电子邮件没有送达。

相反,您可以使用PHPMailer。下面是一些示例代码。 https://github.com/PHPMailer/PHPMailer

这应该可行,但您也可以启用SMTPAuth以及用户名和密码,然后您将以强有力的方式发送电子邮件,这将使您更有可能到达目的地。

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$body = '<p>this is a <strong>test</strong> email</p><p><img src="cid:qrcode" /></p>';

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = false;

    $mail->setFrom('chris@me.com', 'Chris');
    $mail->addAddress('me@gmail.com');

    $mail->isHTML(true);
    $mail->Subject = 'This is a PHPMailer Test';
    $mail->Body    = $body;
    $mail->AltBody = $body;

    $mail->send();

    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}