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 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>


<link rel="stylesheet" type="text/css" href="bighat.css">
<script src="Scripts.js"></script>
<script type="text/javascript">


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

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

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

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

    Place description here

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

             // 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">

            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('', 587);
              $swift = Swift_Mailer::newInstance($transport); 

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

              // attach the body of the email
             $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 =(
                 echo "Something went wrong - ";



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

<script type="text/javascript">



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douyou1960 douyou1960 7年前

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

                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:

                    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:

    点赞 评论 复制链接分享
  • 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: To: 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_=_-- ,

    What information displays for you?

    点赞 评论 复制链接分享