【问题标题】:How to send an email from a form using Send Grid如何使用 Sendgrid 从表单发送电子邮件
【发布时间】:2014-02-12 19:34:02
【问题描述】:

对不起,这个问题似乎是重复的,我见过几个关于通过表单发送电子邮件的问题,但仍然无法解决这个问题。

相关的背景信息:站点在 Azure 上,电子邮件服务是 sendgrid。我在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>

【问题讨论】:

  • 更新问题以反映进度(页面现在加载但电子邮件仍未发送)

标签: php html email swiftmailer sendgrid


【解决方案1】:

对于您的 swift mailer,请检查您的路径并确保它正确指向您需要的文件。

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

【讨论】:

  • 按照您的建议打开错误表明这是一个路径错误。您提出的修复方法有效。我非常感激。
【解决方案2】:

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

消息已发送给 1 位用户感谢您的注册!测试,消息 ID:日期:2014 年 2 月 25 日星期二 21:41:52 +0000 主题:测试发件人:elmer@sendgrid.com 收件人:elmer@thinkingserious.com MIME 版本:1.0 内容类型:multipart/alternative;边界="_=_swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1_=" --=_swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1_=_ 内容类型:文本/纯文本; charset=utf-8 Content-Transfer-Encoding:quoted-printable Testing 1,2,3 --_=_swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1_=_-- , elmer@sendgrid.com

为您显示什么信息?

【讨论】:

  • 我得到的控制台错误是文件有意外结束或
【解决方案3】:

尝试添加

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

在您现有的 PHP 代码之上,提交表单并查看数组包含的内容。 可能是您的 if 语句检查的条件未按预期通过。

【讨论】:

  • 感谢您的提示。提交表单后,它返回了一个数组,该数组成功显示了我的输入。所以我还是很困惑。数组( [from] => test@test.com [subject] => 测试主题 [message] => 测试消息 [submit] => 注册)
猜你喜欢
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多