【发布时间】: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