【问题标题】:I cannot receieve emails even after Debugging PhpMailer即使在调试 PhpMailer 之后我也无法收到电子邮件
【发布时间】:2014-07-06 20:21:49
【问题描述】:

我正在使用 php mailer 作为我的联系表格。尽管我没有收到电子邮件,但我能够收到发送电子邮件的成功消息。 这是我的代码:

contact.php

<form method="post" id="contactForm" action="contactProcess.php">
    <div class="clearfix">
        <div class="grid_6 alpha fll"><input type="text" name="senderName" id="senderName" placeholder="Name *" class="requiredField" /></div>
        <div class="grid_6 omega flr"><input type="text" name="senderEmail" id="senderEmail" placeholder="Email Address *" class="requiredField email" /></div>
    </div>
    <div><textarea name="message" id="message" placeholder="Message *" class="requiredField" rows="8"></textarea></div>
    <input type="submit" id="sendMessage" name="sendMessage" value="Send Email" />
    <span>  </span>
</form>

contactProcess.php

<?php
include 'library.php'; // include the library file
include "classes/class.phpmailer.php"; // include the class name
if(isset($_POST["sendMessage"])){
    $name = $_POST['senderName'];
    $email = $_POST['senderEmail'];
    $message = $_POST['message'];

    $mail   = new PHPMailer; // call the class 
    $mail->IsSMTP(); 
    $mail->SMTPDebug = 2;
    $mail->Host = SMTP_HOST; //Hostname of the mail server
    $mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
    $mail->SMTPAuth = true; //Whether to use SMTP authentication
    $mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
    $mail->Password = SMTP_PWORD; //Password for SMTP authentication
    $mail->From = $email;  //From address of the mail
    $mail->FromName = $name;
    $mail->Subject = ("Mail From Contact Form"); //Subject of your mail

    $mail->IsHTML(true);
    $mail->AddAddress("me@add.com");//To address who will receive this email
    $mail->AddCC("add@add.com");
    $mail->AddReplyTo("add2@add.com", "Me");
    $mail->Body = $message;
    $mail->AltBody = $message;

    $send = $mail->Send(); //Send the mails
    if($send){ ?>
        <script language="javascript" type="text/javascript">
        alert('sent');
        window.location = 'contact.php';
    </script>
    <?php
    }
    else{ ?>
        <script language="javascript" type="text/javascript">
        alert('Message failed');
        window.location = 'contact.php';
    </script>
    <?php
    }
}
?>

最后是 ajax 验证脚本: main.js

// Ajax Contact
if ($("#contactForm")[0]) {
    $('#contactForm').submit(function () {
        $('#contactForm .error').remove();
        $('.requiredField').removeClass('fielderror');
        $('.requiredField').addClass('fieldtrue');
        $('#contactForm span strong').remove();
        var hasError = false;
        $('#contactForm .requiredField').each(function () {
            if (jQuery.trim($(this).val()) === '') {
                var labelText = $(this).prev('label').text();
                $(this).addClass('fielderror');
                $('#contactForm span').html('<strong>*Please fill out all fields.</strong>');
                hasError = true;
            } else if ($(this).hasClass('email')) {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if (!emailReg.test(jQuery.trim($(this).val()))) {
                    var labelText = $(this).prev('label').text();
                    $(this).addClass('fielderror');
                    $('#contactForm span').html('<strong>Your email address is incorrect</strong>');
                    hasError = true;
                }
            }
        });
        if (!hasError) {
            $('#contactForm').slideDown('normal', function () {
                $("#contactForm #sendMessage").addClass('load-color');
                $("#contactForm #sendMessage").attr("disabled", "disabled").val('Sending message. Please wait...');
            });
            var formInput = $(this).serialize();
            $.post($(this).attr('action'), formInput, function (data) {
                $('#contactForm').slideUp("normal", function () {
                    $(this).before('<div class="notification-box notification-box-success"><p><i class="icon-ok"></i>Thanks!</strong> Your email was successfully sent. We will get back to you soonest!.</p></div>');
                });
            });
        }
        return false;
    });
}

我尝试设置 $mail->SMTPDebug = 2;并且还包括 error_reporting(E_ALL);希望我能得到任何错误但没有任何反应。

任何想法,请!

【问题讨论】:

  • 您是否有权访问 Web 服务器中的邮件日志?您的本地邮件配置可能有问题,这些邮件可以在本地接受,但不会离开您的 websrv。如果您在 Linux/UNIX 服务器上工作,并且可以访问其上的 shell 控制台,请尝试使用“mail”命令发送一个,看看它是否到达目的地。
  • 不幸的是,我无法访问共享 Windows 服务器上的邮件日志。有办法解决吗?
  • 我与我的托管解决方案取得了联系,他们说他们已经通过网络邮件测试了该帐户的邮件服务,并且工作正常,因此它必须与脚本有关。

标签: javascript php forms email phpmailer


【解决方案1】:

我在我的服务器上尝试了您的代码,并相应地设置了 SMTP_* 值。在我的例子中,主机是“localhost”、端口 25 和 SMTP Auth “false”。消息已正确排队。

所以我的猜测是邮件设置存在问题。检查所有 SMTP_* 值(我假设您在 library.php 或它包含的文件中设置)是否正确。

评论window.location() 调用,我能够从 SMTP 调试中看到此输出。

SMTP -> FROM SERVER: 220 <server name edited> ESMTP Postfix (Debian/GNU)
SMTP -> FROM SERVER: 250-<server name edited> 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
SMTP -> FROM SERVER: 250 2.1.0 Ok
SMTP -> FROM SERVER: 250 2.1.5 Ok
SMTP -> FROM SERVER: 354 End data with .
SMTP -> FROM SERVER: 250 2.0.0 Ok: queued as C39A6440AA
SMTP -> FROM SERVER: 221 2.0.0 Bye 

总结一下,你的代码看起来还不错,可能是配置有问题。

希望这会有所帮助!

【讨论】:

  • @ Mariano:是的,我在 library.php 中设置 smtp 值,我想相信我使用了正确的值,因为我已经彻底检查了它们。除非,在我的情况下是我设置的另一个邮件帐户的 SMTP_USNAME 不是正确使用的值。(我已经设置它:hello@add.com 并且正在使用电子邮件帐户的密码作为 SMTP_PASSWORD)...你认为这是问题吗?如果不是,我现在唯一担心的是你说你使用了 SMTP Auth“false”..我应该将我的 SMTP Auth“true”更改为 SMTP Auth“false”吗?再次感谢大家的支持!
  • @Josealonso14,这完全取决于服务器设置。在我的测试中,auth 不是必需的,但生产服务器通常不是这种情况。即便如此,由于您的代码仅通过更改配置即可工作,我建议您尝试在另一台服务器上运行它(也许在您的机器上设置本地 LAMP/WAMP?),或使用其他邮件提供商,甚至两者兼而有之。
猜你喜欢
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2011-05-30
  • 2019-01-19
相关资源
最近更新 更多