dongzi9196
dongzi9196
2014-02-12 19:34

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

已采纳

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 douyou1960 7年前

    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

    点赞 评论 复制链接分享
  • donglaogu3788 donglaogu3788 7年前

    Try adding

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

    above your existing PHP code, submit the form and see what the array contains. Chances are the conditions your if statement is checking isn't coming through as expected.

    点赞 评论 复制链接分享
  • duanbodai5166 duanbodai5166 7年前

    I just ran this code on my local machine, sans javascript, and the email was sent. Following is the debugging information that was displayed in the browser:

    Message sent out to 1 usersThanks for signing up! Test, Message-ID: Date: Tue, 25 Feb 2014 21:41:52 +0000 Subject: Test From: elmer@sendgrid.com To: elmer@thinkingserious.com MIME-Version: 1.0 Content-Type: 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

    What information displays for you?

    点赞 评论 复制链接分享

为你推荐