即使邮件函数返回 true,我的服务器也无法发送电子邮件


My server cannot send email even though the mail function is returning true

我正在尝试向我的服务器发送电子邮件。我正在使用php的邮件功能。该函数返回 true。但我没有收到任何电子邮件。我已经检查了日志。甚至日志也没有显示任何错误。我的域名是"islamerkotha.com"。我的代码如下 -

<?php
    $msg = "First line of text'nSecond line of text";
    $msg = wordwrap($msg,70);
    $headers = "From: test1@islamerkotha.com";
    mail("erfan.bashar.13@gmail.com", "My subject", $msg, $headers);

谢谢。

在电子邮件的路径上有很多点,你的可能会失败,但看看 PHP mail() 函数页面; 它特别指出,如果邮件被接受传递,则该函数返回 true,并且"重要的是要注意,仅仅因为邮件被接受传递, 这并不意味着邮件实际上会到达预定目的地。

编辑:查看此处以获取有关PHP错误报告的更多信息。如果您没有看到任何错误,请查看 phpinfo() 以查看是否启用了 mail() 函数。如果是这样,那么是时候开始寻找更下游了......

试试PHP Mailer,有了这个,你不需要有本地的SMTP服务器,你也可以使用你的验证

    <?php
    require 'PHPMailerAutoload.php';
    $mail = new PHPMailer;
    //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->From = 'from@example.com';
    $mail->FromName = 'Mailer';
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }