[英]How to send an email from a form using Send Grid
抱歉,这个问题似乎是重复的,我已经看到一些有关通过表单发送电子邮件的信息,但仍然无法使它正常工作。
必要时的背景信息:网站位于Azure上,电子邮件服务为sendgrid。 我从http://swiftmailer.org/download下载了zip文件,并将其包含在我的项目中。
目标:让用户填写2个字段,接收一封包含该内容和预设主题行的电子邮件。
问题/问题:没有发送电子邮件。 另外,提交表单时,表单按预期消失,但未回显任何内容,页面中已加载js的部分从页面中消失了。
显示的控制台错误是:“意外的”或文件结尾。应在文档结尾之前关闭所有打开的元素。 (指向表格开始前的行)。 我不完全理解为什么会出错,因为在关闭html之前关闭了我的body元素。
我现在可以忍受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>
对于您的快速邮件发件人,请检查您的路径并确保其正确指向您需要的文件。
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
我只是在没有javascript的本地计算机上运行了此代码,然后就发送了电子邮件。 以下是浏览器中显示的调试信息:
消息已发送给1个用户感谢您注册! 测试,消息ID:日期:2014年2月25日,星期二21:41:52 +0000主题:测试自:elmer@sendgrid.com收件人:elmer@thinkingserious.com MIME版本:1.0内容类型:multipart / alternative; boundary =“ _ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1_ = ”- = _swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _内容类型:文本/纯文本; charset = utf-8内容传输编码:带引号的可打印测试1,2,3 --_ = _ swift_v4_1393364512_3e11fe3f62e298d09b07245f43b6dea1 _ = _--,elmer@sendgrid.com
为您显示什么信息?
尝试添加
<pre> <?php print_r( $_POST ); ?> </pre>
在您现有的PHP代码上方,提交表单并查看数组包含的内容。 您的if语句正在检查的条件可能没有按预期通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.