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

时间:2014-02-12 19:34:02

标签: php html email swiftmailer sendgrid

对不起,这个问题似乎有点重复,我看过几个关于通过表单发送电子邮件的问题,但仍然无法让它工作。

相关案例的背景信息:站点在Azure上,电子邮件服务是sendgrid。我在http://swiftmailer.org/download下载了zipfile并将其包含在我的项目中。

目标:让用户填写2个字段,接收包含该内容和预设主题行的电子邮件。

问题/问题:没有发送电子邮件。另外,当提交时,表单按预期消失,但没有任何回显,并且页面的js加载部分从页面中消失。

显示的控制台错误是:“Unexpected”“或文件结束。所有打开的元素应在文档结束前关闭。” (指向表格开始之前的行)。我不完全理解为什么这是一个错误,因为在关闭html之前我的body元素已关闭。

我现在可以忍受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>

3 个答案:

答案 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代码出错时你不会得到有意义的错误信息。一旦您的网站准备好生产,请务必将其关闭!

对于Azure,您无法直接打开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

答案 1 :(得分:1)

我刚刚在我的本地计算机上运行此代码,没有javascript,并且已发送电子邮件。以下是浏览器中显示的调试信息:

发送给1位用户的消息谢谢注册!测试,消息ID:日期:2014年2月25日星期二21:41:52 +0000主题:测试来自:elmer@sendgrid.com致:elmer@thinkingserious.com MIME-版本:1.0内容类型:multipart / alternative; boundary =“_ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = ” - = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _ Content-Type:text / plain; charset = utf-8 Content-Transfer-Encoding:quoted-printable Testing 1,2,3 --_ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _--,elmer @ sendgrid.com

显示哪些信息?

答案 2 :(得分:0)

尝试添加

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

在您现有的PHP代码之上,提交表单并查看数组包含的内容。 机会是你的if语句检查没有达到预期的条件。

相关问题