我正在尝试使用PHPMailer将联系表单中的数据输入发送到另一个电子邮件地址。我跟着Build A PHP Contact Form,但似乎无法让它发挥作用。 这是我的index.php
<?php
session_start();
require_once 'helpers/security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contact Form</title>
<link rel="stylesheet" href="css/main.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="name" autocomplete="off" <?php echo isset($fields['name']) ? ' value="' . e($fields['name']) . '"' : '' ?>>
</label>
<label>
Your email address *
<input type="text" name="email" 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>
</form>
</div>
</body>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
这是我的contact.php
<?php
session_start();
require_once 'libs/phpmailer/PHPMailerAutoload.php';
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'message' => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)) {
$errors[] = 'The ' . $field . ' field is required.';
}
}
if(empty($errors)) {
$mail = new PHPMailer;
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'email@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->isHTML(true);
$mail->Subject = 'Contact form submitted';
$mail->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';
$mail->FromName = 'Contact';
$mail->AddAddress('email@gmail.com', 'name');
if($mail->send()) {
header('Location: thanks.php');
} else {
$errors[] = 'Sorry, could not send email. Try again later.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: index.php')
?>
当我尝试提交联系表单时,我收到错误&#34;抱歉,无法发送电子邮件。稍后再试。&#34;谁能帮我弄清楚出了什么问题?提前致谢
当我使用print_r($ mail)时,它会输出以下内容:
2015-04-03 12:37:40 CLIENT -> SERVER: EHLO 127.0.0.1 2015-04-03 12:37:40 CLIENT -> SERVER: STARTTLS
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in C:\xampp\htdocs\contact_form\libs\phpmailer\class.smtp.php on line 339
2015-04-03 12:37:40 CLIENT -> SERVER: QUIT 2015-04-03 12:37:40 SMTP ERROR: QUIT command failed: 2015-04-03 12:37:40 SMTP connect() failed.
PHPMailer Object ( [Version] => 5.2.9 [Priority] => 3 [CharSet] => iso-8859-1 [ContentType] => text/html [Encoding] => 8bit [ErrorInfo] => SMTP connect() failed. [From] => root@localhost [FromName] => Contact [Sender] => [ReturnPath] => [Subject] => Contact form submitted [Body] => From: Your name field (your email address field)
your message field
[AltBody] =>
[Ical] =>
[MIMEBody:protected] => From: Your name field (your email address field)
your message field
[MIMEHeader:protected] => Date: Fri, 3 Apr 2015 14:37:40 +0200 To: Name From: Contact Subject: Contact form submitted Message-ID: <9c632471cedadb7f5e7ee6687ee9d5b7@127.0.0.1> X-Priority: 3 X-Mailer: PHPMailer 5.2.9 (https://github.com/PHPMailer/PHPMailer/) MIME-Version: 1.0 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit
[mailHeader:protected] =>
[WordWrap] => 0
[Mailer] => smtp
[Sendmail] => /usr/sbin/sendmail [UseSendmailOptions] => 1
[PluginDir] =>
[ConfirmReadingTo] =>
[Hostname] =>
[MessageID] =>
[MessageDate] => Fri, 3 Apr 2015 14:37:40 +0200
[Host] => smtp.gmail.com
[Port] => 587
[Helo] =>
[SMTPSecure] => tls
[SMTPAuth] => 1
[Username] => email@gmail.com
[Password] => password
[AuthType] =>
[Realm] =>
[Workstation] =>
[Timeout] => 300
[SMTPDebug] => 1
[Debugoutput] => echo
[SMTPKeepAlive] =>
[SingleTo] =>
[SingleToArray] => Array ( )
[do_verp] =>
[AllowEmpty] =>
[LE] =>
[DKIM_selector] =>
[DKIM_identity] =>
[DKIM_passphrase] =>
[DKIM_domain] =>
[DKIM_private] =>
[action_function] =>
[XMailer] =>
[smtp:protected] => SMTP Object ( [Version] => 5.2.9 [SMTP_PORT] => 25 [CRLF] => [do_debug] => 1 [Debugoutput] => echo [do_verp] => [Timeout] => 300 [Timelimit] => 300 [smtp_conn:protected] => [error:protected] => Array ( ) [helo_rply:protected] => [server_caps:protected] => [last_reply:protected] => )
[to:protected] => Array ( [0] => Array ( [0] => email@gmail.com [1] => Name ) )
[cc:protected] => Array ( )
[bcc:protected] => Array ( )
[ReplyTo:protected] => Array ( )
[all_recipients:protected] => Array ( [email@gmail.com] => 1 )
[attachment:protected] => Array ( )
[CustomHeader:protected] => Array ( )
[lastMessageID:protected] => <9c632471cedadb7f5e7ee6687ee9d5b7@127.0.0.1>
[message_type:protected] => plain
[boundary:protected] => Array ( [1] => b1_9c632471cedadb7f5e7ee6687ee9d5b7 [2] => b2_9c632471cedadb7f5e7ee6687ee9d5b7 [3] => b3_9c632471cedadb7f5e7ee6687ee9d5b7 )
[language:protected] => Array ( [authenticate] => SMTP Error: Could not authenticate. [connect_host] => SMTP Error: Could not connect to SMTP host. [data_not_accepted] => SMTP Error: data not accepted. [empty_message] => Message body empty [encoding] => Unknown encoding: [execute] => Could not execute: [file_access] => Could not access file: [file_open] => File Error: Could not open file: [from_failed] => The following From address failed: [instantiate] => Could not instantiate mail function. [invalid_address] => Invalid address [mailer_not_supported] => mailer is not supported. [provide_address] => You must provide at least one recipient email address. [recipients_failed] => SMTP Error: The following recipients failed: [signing] => Signing Error: [smtp_connect_failed] => SMTP connect() failed. [smtp_error] => SMTP server error: [variable_set] => Cannot set or reset variable: )
[error_count:protected] => 1
[sign_cert_file:protected] =>
[sign_key_file:protected] =>
[sign_key_pass:protected] =>
[exceptions:protected] => )
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\contact_form\contact.php:53) in C:\xampp\htdocs\contact_form\contact.php on line 63
答案 0 :(得分:0)
我想出了怎么做。我对我的contact.php进行了以下更改
<?php
session_start();
require_once 'libs/phpmailer/PHPMailerAutoload.php';
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'message' => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)) {
$errors[] = 'The ' . $field . ' field is required.';
}
}
if(empty($errors)) {
$mail = new PHPMailer();
$mail->IsSMTP();
//$mail->SMTPAuth = true;
$mail->SMTPDebug = 1;
$mail->Host = 'aspmx.l.google.com' ;
//$mail->Username = 'email@gmail.com';
//$mail->Password = 'password';
//$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->isHTML(true);
$mail->Subject = 'Contact form submitted';
$mail->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';
$mail->FromName = 'Contact';
$mail->AddAddress('email@gmail.com', 'Name');
if($mail->send()) {
header('Location: thanks.php');
} else {
$errors[] = 'Sorry, could not send email. Try again later.';
//print_r($mail);
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: index.php')
?>