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>