PHPMailer没有发送,没有给错误抛出500错误

时间:2014-09-01 23:18:25

标签: php email openshift phpmailer sendgrid

更新:这是我收到的错误:SMTP connect()失败。 致命错误:未捕获的异常' phpmailerException'邮件' SMTP connect()失败。'

我正在使用一个名为Frameworx的预先存在的php框架。一切都很棒,但它不会发送我相信的电子邮件,因为我使用的是OpenShift,他们对邮件服务器很严格。我需要使用SendGrid,所以我试图使用PHPMailer来做到这一点。也许我说这一切都错了。但基本上当我尝试发送邮件时,我得到的只是一个空白页面。用户已创建。

这就是我所拥有的:

        // Create a New Account Form
    if (isset($_POST['submit']) && $_POST['submit'] == 'newAccount') {
        // User Validations
        if($_POST['usersName'] == '') {
            $msgBox = alertBox("Your First and Last Name is required.", "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['usersEmail'] == '') {
            $msgBox = alertBox("A valid Email Address is required.", "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['password'] == '') {
            $msgBox = alertBox("A New Account Password is required.", "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['password'] != $_POST['passwordr']) {
            $msgBox = alertBox("Passwords do not match, please check your entries.", "<i class='fa fa-times-circle'></i>", "danger");
        // Black Hole Trap to help reduce bot registrations
        } else if($_POST['captcha'] != '') {
            $msgBox = alertBox("An Error was encountered and the New Account could not be created.", "<i class='fa fa-times-circle'></i>", "danger");
        } else {
            // Set some variables
            $dupEmail = '';
            $newEmail = $mysqli->real_escape_string($_POST['usersEmail']);

            // Check for Duplicate email
            $check = $mysqli->query("SELECT 'X' FROM users WHERE usersEmail = '".$newEmail."'");
            if ($check->num_rows) {
                $dupEmail = 'true';
            }

            // If duplicates are found
            if ($dupEmail != '') {
                $msgBox = alertBox("There is all ready a User Account registered with that Email Address.", "<i class='fa fa-times-circle'></i>", "danger");
            } else {
                // Create the new account
                $password = encryptIt($_POST['password']);
                $usersName = $mysqli->real_escape_string($_POST['usersName']);
                $joinDate = date("Y-m-d H:i:s");
                $hash = md5(rand(0,1000));
                $isActive = '0';

                $stmt = $mysqli->prepare("
                                    INSERT INTO
                                        users(
                                            usersEmail,
                                            password,
                                            usersName,
                                            joinDate,
                                            hash,
                                            isActive
                                        ) VALUES (
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?
                                        )");
                $stmt->bind_param('ssssss',
                    $newEmail,
                    $password,
                    $usersName,
                    $joinDate,
                    $hash,
                    $isActive
                );
                $stmt->execute();

                // Send out the email in HTML
                require_once('class.phpmailer.php');

                $mail = new PHPMailer(true);

                $mail->SMTPDebug = 2;

                //Ask for HTML-friendly debug output
                $mail->Debugoutput = 'html';


                $mail->isSMTP();                                     // Set mailer to use SMTP
                $mail->Host = 'https://api.sendgrid.com/';  // Specify main and backup SMTP servers
                $mail->SMTPAuth = true;                               // Enable SMTP authentication
                $mail->Username = 'looloobs';                 // SMTP username
                $mail->Password = 'passowrd';                           // SMTP password
                $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 587;                                    // TCP port to connect to             // TCP port to connect to

                $mail->From = 'militarypropertyproject@gmail.com';
                $mail->FromName = 'Mailer';
                $mail->addAddress('laurenrothlisberger@gmail.com', 'Joe User');     // Add a recipient
                $mail->addReplyTo('info@example.com', 'Information');

                $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';
                }

                $stmt->close();


                // Clear the Form of values
                $_POST['usersName'] = $_POST['usersEmail'] = $_POST['password'] = $_POST['passwordr'] = $_POST['captcha'] = '';
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您似乎尝试通过SMTPSimple Mail Transfer Protocol)发送,但是,您已将SendGrid的Web API域作为主机名传递。因此当phpmailer尝试连接到&#34;域时#34; https://api.sendgrid.com/它失败了。

要解决此问题,请更改此行:

$mail->Host = 'https://api.sendgrid.com/';  // Specify main and backup SMTP servers

要:

$mail->Host = 'smtp.sendgrid.net';  // Specify main and backup SMTP servers

答案 1 :(得分:-2)

$mail->IsMail();

你必须使用这个

相关问题