微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHPMailer 用于联系表单并托管在 Heroku 上发布请求给出 200 但邮件没有发送

如何解决PHPMailer 用于联系表单并托管在 Heroku 上发布请求给出 200 但邮件没有发送

我正在编写联系表格以收集数据。

我的应用在 Heroku 上。它是一个 PHP 应用程序。也配置了 SSL。

据我所知,PHPMailer 配置正确。以下是执行此操作的 PHP 代码。 (联系.PHP)

<?PHP require 'PHPMailer-master/PHPMailerAutoload.PHP'; 
$fromEmail = 'myemail@gmail.com';
$fromName = 'Application Form';
$sendToEmail = 'myotheremmail@gmail.com';
$sendToName = 'Applied';
$subject = 'New message from contact form';

// smtp credentials and server

$smtpHost = 'smtp.gmail.com';
$smtpUsername = 'myemail@gmail.com';
$smtpPassword = 'passwordformyemail';

$fields = array('name' => 'Name','linkedin' => 'LinkedIn URL','phone' => 'Phone Number','email' => 'Email','message' => 'Cover Letter','notice' => 'Notice Period','salary' => 'Salary Expectation');

// message that will be displayed when everything is OK
$okMessage = 'Contact form successfully submitted. Thank you,I will get back to you soon!';

// If something goes wrong,we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

error_reporting(E_ALL & ~E_NOTICE);

try {
    if (count($_POST) == 0) {
        throw new \Exception('Form is empty');
    }
    
    $emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
    $emailTextHtml .= "<table>";

    
    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array,include it in the email
        if (isset($fields[$key])) {
            $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";

        }
    }
    $emailTextHtml .= "</table><hr>";
    $emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Me</p>";

    $mail = new PHPMailer;

    $mail->isSMTP();

    $mail->Host = 'smtp.gmail.com';

    $mail->Port = 587;

    $mail->SMTPSecure = 'tls';
    
    $mail->SMTPAuth = true;
    
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = $smtpUsername;
    
    //Password to use for SMTP authentication
    $mail->Password = $smtpPassword;

    $mail->setFrom($fromEmail,$fromName);
    $mail->addAddress($sendToEmail,$sendToName);
    $mail->addReplyTo($fromEmail,$fromName);

    $mail->isHTML(true);
    
    $mail->Subject = $subject;
    $mail->Body    = $emailTextHtml;
    $mail->msgHTML($emailTextHtml); 
    

    $mail->SMTPDebug = 2;
    $mail->Debugoutput = 'html';

    if (!$mail->send()) {
        throw new \Exception('I Could not send the email.' . $mail->ErrorInfo);
    }
    
    $responseArray = array('type' => 'success','message' => $okMessage);
} catch (\Exception $e) {
    // $responseArray = array('type' => 'danger','message' => $errorMessage);
    $responseArray = array('type' => 'danger','message' => $e->getMessage());
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);
    
    header('Content-Type: application/json');
    
    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}

这里是联系表单代码(index.PHP),

                <form id="contact-form" method="post" action="contact.PHP" role="form">

                    <div class="messages"></div>

                    <div class="controls">

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_name">Name *</label>
                                    <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your Name *" required="required" data-error="Name is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_linkedin">LinkedIn URL</label>
                                    <input id="form_linkedin" type="text" name="linkedin" class="form-control" placeholder="Please enter your LinkedIn profile URL" data-error="LinkedIn URL is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_email">Email *</label>
                                    <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_phone">Phone *</label>
                                    <input id="form_phone" type="tel" name="phone" required="required" class="form-control" placeholder="Please enter your phone number" data-error="Valid Phone number is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_email">required Notice Period *</label>
                                    <input id="form_email" type="text" name="notice" class="form-control" placeholder="Please enter the required period of notice to prevIoUs work place *" required="required" data-error="Notice Period detail is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_salary">Salary Expectation *</label>
                                    <input id="form_salary" type="number" name="salary" class="form-control" placeholder="Please enter your expected salary in Sri Lankan Rupees *" required="required" data-error="Salary expectation is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="form_message">Cover Letter *</label>
                                    <textarea id="form_message" name="message" class="form-control" placeholder="Please write Cover Letter *" rows="4" required="required" data-error="Cover Letter is required"></textarea>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <input type="submit" class="btn btn-success btn-send" value="Submit Application">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <p class="text-muted">
                                    <strong>*</strong> These fields are required.</p>
                            </div>
                        </div>
                    </div>

                </form>

然后我有JS代码(index.js)这是在index.PHP中作为外部脚本添加的,

    $(function () {

    $('#contact-form').validator();


    // when the form is submitted
    $('#contact-form').on('submit',function (e) {

        // if the validator does not prevent form submit
        if (!e.isDefaultPrevented()) {
            var url = "contact.PHP";
            // POST values in the background the the script URL
            $.ajax({
                type: "POST",url: url,data: $(this).serialize(),success: function (data)
                {
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;

                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    
                    // If we have messageAlert and messageText
                    if (messageAlert && messageText) {
                        // inject the alert to .messages div in our form
                        $('#contact-form').find('.messages').html(alertBox);
                        // empty the form
                        $('#contact-form')[0].reset();
                    }
                }
            });
            return false;
        }
    })
});

填写并提交表单时,Heroku 应用程序日志给出以下内容

2021-07-18T10:47:27.738111+00:00 app[web.1]: 10.181.143.206 - - [18/Jul/2021:10:47:27 +0000] "POST /applications/contact.PHP HTTP/1.1" 200 2177 "https://www.example.com/applications/" "Mozilla/5.0 (Linux; Android 11; GM1910) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36

由于我使用的是 gmail SMTP,

  • 我已关闭两因素身份验证
  • 我开启了不太安全的访问

错误日志中也没有任何内容

我为 PHPMailer 添加了以下文件

我尝试按照网络上提供的教程检查问题,但没有成功。

在这里犯了什么愚蠢的错误,或者我错过了关于 PHPMailer 和 Heroku 的一个重要点,这使得它不起作用?

非常感谢任何帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。