2014-09-01 148 views
0

更新:這是我收到的錯誤:SMTP連接()失敗。 致命錯誤:未收到異常'phpmailerException',並顯示消息'SMTP連接()失敗。'PHPMailer沒有發送,沒有給出錯誤拋出500錯誤

我正在使用一個名爲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 = '[email protected]'; 
       $mail->FromName = 'Mailer'; 
       $mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
       $mail->addReplyTo('[email protected]', '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

如果您正在爲您的標題所暗示的,然後去檢查服務器的錯誤日誌第一的得到一個500內部服務器錯誤所有。 – CBroe 2014-09-01 23:30:26

+0

我承認並不知道很多關於服務器日誌的知識,但是當我進去時,這就是我所看到的。 「[01/Sep/2014:19:48:18 -0400]」POST /login.php HTTP/1.1「500 - 」http://scoutmember-milpropertyproj.rhcloud.com/login.php「」Mozilla/5.0 Macintosh;英特爾Mac OS X 10.9; rv:31.0)Gecko/20100101 Firefox/31.0「 – looloobs 2014-09-01 23:51:47

+1

該行將來自服務器的_access_日誌... __error__日誌是不同的 – CBroe 2014-09-01 23:54:50

回答

0

它看起來像你正試圖通過SMTP(在Simple Mail Transfer Protocol)發送,但是,你傳遞在SendGrid的Web API域中作爲主機名。因此,當phpmailer試圖連接到「域」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 
+0

感謝過去的php郵件程序我使用過之前的,但那是問題所在。 – looloobs 2014-09-02 20:40:00

-2
$mail->IsMail(); 

,你必須使用這個

+0

OP很明顯使用'IsSMTP()' – Phil 2014-09-02 02:10:37

+0

@Shen:你真的需要更加關注你正在回答的問題 – 2014-09-03 17:27:18