dongzi9196 2014-02-12 19:34
浏览 47
已采纳

如何使用发送网格从表单发送电子邮件

Sorry this question seems like a duplicate, I've seen several regarding sending an e-mail through a form, but still have not been able to get this to work.

Background information in case it is relevant: Site is on Azure, e-mail service is sendgrid. I downloaded the zipfile at http://swiftmailer.org/download and included it in my project.

Objective: Have user fill out 2 fields, receive an e-mail with that content and a preset subject line.

Question/Problem: No e-mail is sent. In addition, when submitted the form goes away as expected, but nothing is echoed, and the parts of the page which are js loaded disappear from the page.

The console error displayed is: "Unexpected "" or end of file. All open elements should be closed before the end of the document." (points to line just before the form begins). I do not fully understand why this is an error since my body element is closed before closing the html.

I can live with the js issue for now, but if anyone can help me understand why no e-mail is sent/nothing is echoed it would be appreciated.

Thanks in advance!

<!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>
  • 写回答

3条回答 默认 最新

  • douyou1960 2014-02-26 18:43
    关注

    For your swift mailer include please check your pathing and ensure it is correctly pointing to the file you need.

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

    For example if Sendgrid is a folder in your root directory, you wont need the .. it might only be the following:

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

    You also mentioned your running an Azure Website, something to take note of is Azure Websites have error messages turned off by default for security reasons(as they should) but if your site is still in development then you should turn custom errors on or you won't ever get a meaningful error message when things go wrong in your php code. Make sure to turn it off once your site is ready for production!

    For Azure you can't directly turn on errors in the php.ini file since they restrict this but you have to put a custom .user.ini file in your root directory with the following

    display_errors = On
    

    This is why I suspect your site wasn't telling you anything is wrong. More info on .user.ini files here at this link: http://blogs.msdn.com/b/silverlining/archive/2012/07/10/configuring-php-in-windows-azure-websites-with-user-ini-files.aspx

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗