2014-02-12 36 views
0

對不起,這個問題看起來像是重複的,我已經看過幾個關於通過表單發送電子郵件,但仍然無法使其發揮作用的問題。如何使用發送表格發送表單中的電子郵件

相關情況下的背景信息:網站位於Azure上,電子郵件服務爲sendgrid。我下載了zip文件http://swiftmailer.org/download,並將其包含在我的項目中。

目標:讓用戶填寫2個字段,接收包含該內容和預設主題行的電子郵件。

問題/問題:未發送電子郵件。另外,提交時,表單按預期消失,但沒有任何迴應,並且加載了js的頁面部分從頁面中消失。

顯示的控制檯錯誤是:「意外」或文件結束,所有打開的元素應在文檔結束之前關閉。 (指向表單開始前的行)。我不完全理解爲什麼這是一個錯誤,因爲我的body元素在關閉html之前是關閉的。

我現在可以忍受js問題,但如果有人能幫助我理解爲什麼沒有發送電子郵件/沒有任何迴應,將不勝感激。

在此先感謝!

<!DOCTYPE html> 
<html> 

<head> 

<link rel="stylesheet" type="text/css" href="bighat.css"> 
<script src="Scripts.js"></script> 
<script type="text/javascript"> 
validateCookie(); 
</script> 
</head> 

<body> 

    <header class="header" id="header"> 
     <!--Loaded by Script--> 
    </header> 


    <nav class="menu" id="menu"> 
     <!-- Loaded by Script--> 
    </nav> 


    <section class="outerpiccontainer"> 
     <p> Place photo here</p> 
    </section> 

    <section class="description"> 
    <h2> About Us </h2> 

<p> 
    Place description here 
</p> 

    <h4>Sign Up</h4> 
<p> 
    If you would like to join our mailing list and receive updates on new brews and store availability please enter your e-mail below: 
</p> 

<div> 
<?php 
      // display form if user has not clicked submit 
    if (!isset($_POST["submit"])) 
    { 
    ?> 
     <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>"> 
     From: <input type="text" name="from"><br> 
     Subject: <input type="text" name="subject"><br> 
     Message: <textarea rows="5" cols="40" name="message"></textarea><br> 
     <input class="button" type="submit" name="submit" value="Sign Up"> 
     </form> 
    <?php 
    } 

    else 
      include_once "../Sendgrid/lib/swift_required.php"; 
      // the user has submitted the form 
     { 
     // Check if the "from" input field is filled out 
     if (isset($_POST["from"])) 
      { 
       $from = $_POST["from"]; // sender 
       $subject = $_POST["subject"]; 
       $text = $_POST["message"]; 
        // message lines should not exceed 70 characters (PHP rule), so wrap it 
       $text = wordwrap($text, 70); 

       //send to 
       $to = "my e-mail"; 


       // Login credentials 
       $username = 'my sendgrid username'; 
       $password = 'my sendgrid pw'; 


       // Setup Swift mailer parameters -- When included JS fails to load after Submit 
       $transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587); 
       $transport->setUsername($username); 
       $transport->setPassword($password); 
       $swift = Swift_Mailer::newInstance($transport); 


       // Create a message (subject) 
       $message = new Swift_Message($subject); 

       // attach the body of the email 
      $message->setFrom($from); 
      $message->setTo($to); 
      $message->addPart($text, 'text/plain'); 


       // send mail 
      if ($recipients = $swift->send($message, $failures)) 
       { 
      // This will let us know how many users received this message 
       echo 'Message sent out to '.$recipients.' users'; 
       echo "Thanks for signing up! $subject, $message, $from"; //Nothing being echoed 
       } 
      // something went wrong =(
      else 
       { 
       echo "Something went wrong - "; 
       print_r($failures); 
       } 
     } 
    } 
?> 

    </div> 

</section> 

<footer id="footer"> 
<!-- Loaded by Script--> 
</footer> 

<script type="text/javascript"> 

getMenu(); 
getHeader(); 
getFooter(); 

</script> 
</body> 

</html> 
+0

更新問題以反映進度(頁面現在加載但電子郵件仍未發送) –

回答

3

爲了您的迅捷郵件包括請檢查您的路徑,並確保它正確指向您需要的文件。

else 
      include_once "../Sendgrid/lib/swift_required.php"; 
      // the user has submitted the form 

例如,如果Sendgrid是在你的根目錄下的一個文件夾,你不會需要..它可能只是以下幾點:

else 
       include_once "Sendgrid/lib/swift_required.php"; 
       // the user has submitted the form 

您也提到您運行Azure的網站,東西請注意,Azure網站出於安全原因(如果他們應該)默認關閉了錯誤消息,但是如果您的網站仍在開發中,那麼您應該開啓自定義錯誤或者在事情發生時不會收到有意義的錯誤消息你的php代碼錯了。確保在您的網站準備好生產後關閉它!

爲天青你不能直接把錯誤的php.ini文件,因爲它限制了這一點,但你必須把自定義的.user.ini文件在你的根目錄下有以下

display_errors = On 

這就是爲什麼我懷疑你的網站沒有告訴你任何問題。有關.user.ini文件的更多信息,請點擊此鏈接:http://blogs.msdn.com/b/silverlining/archive/2012/07/10/configuring-php-in-windows-azure-websites-with-user-ini-files.aspx

+0

打開錯誤,如您所示,顯示它是一個路徑錯誤。您建議的修補程序有效我非常感激。 –

0

嘗試增加

<pre> <?php print_r($_POST); ?> </pre>

上述現有的PHP代碼,提交表單,看看數組包含。 機會是您的if語句正在檢查的條件未按預期達到。

+0

感謝您的提示。提交表單後,它返回一個數組,它已成功顯示我的輸入。所以我仍然難倒。 陣列 ( [從] => [email protected] [主題] =>測試對象 [消息] =>測試消息 [提交] =>註冊 ) –

1

我剛剛在我的本地機器上運行此代碼,沒有JavaScript,並且發送了電子郵件。以下是瀏覽器中顯示的調試信息:

發送給1位用戶的消息感謝您註冊!測試,消息ID:日期:2014年2月25日星期二21:41:52 +0000主題:測試從:[email protected]到:[email protected] MIME版本:1.0內容類型:multipart/alternative; border =「_ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1_ = 」 - = _swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _ Content-Type:text/plain;字符集= UTF-8的內容傳輸編碼:引用可打印測試1,2,3 --_ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _--,[email protected]

爲您做什麼的信息顯示?

+0

控制檯錯誤我得到的是該文件有意想不到的結果或。我一直無法從服務器獲取調試信息(Azure隱藏ini) –

+0

所以問題出現在我的路徑中,根據我當時的代碼無法確定問題,但您的答案顯示我的代碼是正確的,並導致一張海報向我展示如何在Azure上啓用錯誤,所以感謝您的幫助。 –

+0

很高興成爲服務:) –