對不起,這個問題看起來像是重複的,我已經看過幾個關於通過表單發送電子郵件,但仍然無法使其發揮作用的問題。如何使用發送表格發送表單中的電子郵件
相關情況下的背景信息:網站位於Azure上,電子郵件服務爲sendgrid。我下載了zip文件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>
更新問題以反映進度(頁面現在加載但電子郵件仍未發送) –