【发布时间】:2014-11-17 06:44:28
【问题描述】:
我目前正在使用 XAMPP 和 PHPMailer 在我的测试网站上创建一个简单的“联系我”电子邮件表单。问题如下。我似乎无法发送测试电子邮件以确保它是否正常工作。我周末的大部分时间都在试图找出这个问题的根源,但我似乎仍然找不到答案。它甚至没有告诉我是否出了问题。
我根据 web/StackOverFlow/ 文档尝试了以下方法:
- 从 php.ini 中删除了“extension=php_openssl.dll”行的分号
- 重新启动 XAMMP
- 更新的端口和主机值*见下文*
- 来自 https://github.com/PHPMailer/PHPMailer 的 DLd PHPMailer
- 在 Youtube 上观看 PHPAcademy 的“Build A PHP Contact Form”视频
- 实际上他 95% 的代码都是他的...只是对我不起作用
- 对他的视频发表了评论。暂无回复。
我浏览了该网站上的大部分问题,但没有找到任何可以解决我的问题的方法。到目前为止,我正在阅读一些文档。我似乎在做正确的事。因此,让我感到困惑的是,这种简单的形式不适合我。
contact.php
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link rel="stylesheet" href="indexContactStyle.css">
</head>
<body>
<div class="contact">
<?php if( !empty($errors)) : ?>
<div class="panel">
<ul> <li> <?php echo implode('</li><li>', $errors ); ?> </li> </ul>
</div>
<?php endif ?>
<form action="contact.php" method="post">
<label>
Your Name * <input type="text" name="inputName" autocomplete="off" <?php echo isset($fields['name']) ? ' value= " '. e($fields['name']) .' " ' : '' ?> >
</label>
<label>
Your Email * <input type="text" name="inputEmail" autocomplete="off" <?php echo isset($fields['email']) ? ' value= " '. e($fields['email']) .' " ' : '' ?> >
</label>
<label>
Your Message * <textarea name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
</label>
<input type="submit" value="Send">
<p class="muted"> * means a required field</p>
<button class="muted" type="button" onClick="goBack()">Back</button>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
index.php
<?php
session_start();
require_once 'libs/phpMailer/PHPMailerAutoload.php';
$errors = [];
print_r($_POST);
if (isset($_POST['inputName'], $_POST['inputEmail'], $_POST['message'])) {
$fields = [
'name' => htmlentities( $_POST['inputName'] ),
'email' => $_POST['inputEmail'],
'message'=> $_POST['message']
];
foreach ($fields as $field => $data) {
if( empty($data) ) {
$errors[] = 'The ' . $field . 'field is required.';
}
}
if(empty($errors)) {
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPauth = true;
$m->SMTDebug = true;
$m->Host ='smtp.gmail.com';
$m->Username = 'myEmail@gmail.com';
$m->Password = 'myPass';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML(true);
$m->Subject = 'Contact form submitted';
$m->Body ='From: ' .$fields['name'] . ' ( ' . $fields['email'] . ' ) <p> ' . $fields['message'] . ' </p> ' ;
$m->FromName = 'Contact';
// Not entirely sure what is supposed to go here. Some ppl put their own email others the email of the sender. which one is it?
$m->AddAddress('myEmail@gmail.com', 'myName');
if ( $m->send() ) {
header('Location: thanks.php');
die();
} else {
$errors[] = 'Sorry could not send email. Try again later ';
}
}
} else {
$errors[] = 'Something went HORRIBLY WRONG!!';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: contact.php');
【问题讨论】:
标签: gmail xampp phpmailer html-entities contact-form