我通过GoDaddy上传了一个带有附件选项的联系表单页面,其中带有嵌入式php(contact.php)的html编码,但是当我想填写表单并提交时,我收到了很多错误。不明白
我正在从我的联系页面使用php邮件程序和php包含到另一个php页面(bootstrap.php)。
以下是代码:
联系页面:
<!DOCTYPE html>
<html>
<head>
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
include('bootstrap.php');
?>
</head>
<body>
<div class = "wrapper">
<section class ="bulk">
<?php if(isset($_GET["status"]) == "thanks") { ?>
<br><br><br><br><br><br><br><br><br><br><br><br>
<p class = "text-center"> Thanks for the email! We will be in touch shortly </p>
<?php } else { ?>
<h1 class = "text-center">
We look forward to working with you!
</h1><br>
<h4 class = "text-center" style="font-weight:normal;">
Please complete the form and attach file
</h4>
<!-- form stuff -->
<div class ="row">
<div class="contact">
<form method ="post" action ="contact.php" enctype='multipart/form-data'>
<div class="form-group">
<label for="name">Name</label>
<input type ="text" class="form-control" name="name" id ="name" placeholder="Enter name" value="<?php if (!empty($_POST['name'])) {echo $_POST['name'];} ?>">
<?php
if ($error) {
echo "<p class='text-danger'>$errName</p>";
}
else {
echo "<br>";
}
?>
</div>
<!--email-->
<div class="form-group">
<label for="email">Email</label>
<input type ="text" class="form-control" name="email" id ="email" placeholder="Enter email" value="<?php if (!empty($_POST['email'])) {echo $_POST['email'];} ?>">
<?php
if ($error) {
echo "<p class='text-danger'>$errEmail</p>";
}
else {
echo "<br>";
}
?>
</div>
<!--msg-->
<div class="form-group">
<label for="message">Message</label>
<textarea name ="message" class="form-control" id ="message" placeholder="Enter message" rows = "10" ></textarea>
<?php
if ($error) {
echo "<p class='text-danger'>$errMessage</p>";
}
else {
echo "<br>";
}
?>
</div>
<!--attachments-->
<div class="form-group">
<label for="attachment" >Attach your file</label>
<input type ="file" name='attachment' id='attachment'>
</div>
<!--Hpam Sponypot -->
<div class="form-group" style="visibility: hidden">
<label for="address">Address</label>
<input type ="text" name="address" id ="address">
<p> Humans, do not fill out this form! </p>
</div>
<!--attachments-->
<div class = "buttons">
</div><button type="submit" value="Send" class="button">Submit</button>
</form>
</div><br><br>
</div>
<?php } ?>
</section>
这是包含在联系页面顶部的php页面(bootstrap.php):
require_once("phpmailer/class.phpmailer.php");
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new phpmailer();
// VARIABLE DECLARATIONS
$errName = '';
$errEmail = '';
$errMessage = '';
$error = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]); //trim destroys whitespaces like "Tab" or "Spacebar"
$email = trim($_POST["email"]);
$phonenumber = trim($_POST["phonenumber"]);
$message = trim($_POST["message"]);
// Attack #1 preventor - Spam Honeypot
if ($_POST["address"] != "") {
echo "SPAM HONEYPOT";
exit;
}
// Attack #2 preventor - Email header injection hack preventor
foreach($_POST as $value) {
if(stripos($value, 'Content-Type:') !== FALSE) {
echo "There was a problem with the information you entered.";
exit;
}
}
// Check if name has been entered
if ($name == "") {
$errName = 'Sorry, you have not entered a name, please try again.';
}
// Check if message has been entered
if ($message == "") {
$errMessage = 'Sorry, you have not typed a message.';
}
// Check if email is valid
if (!$mail->validateAddress($email)) {
$errEmail = 'Sorry, please enter a valid email address.';
}
// Flag error messages if any tests fail
if ($errName || $errMessage || $errPhonenumber || $errEmail) {
$error = true; // Boolean to flag validating error messages in the HTML
}
else {
// EMAIL BODY IFF FORM VALIDATION IS SUCCESSFULL!
$email_body = "";
$email_body = $email_body. "Name: " . $name . "<br>";
$email_body = $email_body. "Email: " . $email . "<br>";
$email_body = $email_body. "Phone number: " . $phonenumber . "<br>";
$email_body = $email_body. "Message: " . $message;
date_default_timezone_set('Etc/UTC');
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "#############################";
//Password to use for SMTP authentication
$mail->Password = "###################";
//Set who the message is to be sent from
$mail->setFrom($email, $name);
//Set who the message is to be sent to
$mail->addAddress('companyname@gmail.com', 'Company Name');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Add attachment
$mail->addAttachment($_FILES['attachment']);
// if (isset($_FILES['attachment']) &&
// $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
// $mail->addAddress($_FILES['attachment']['tmp_name'],
// $_FILES['attachment']['name']);
// }
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($email_body);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
}
header("Location: contact.php?status=thanks");
exit;
}
}
?>
这些是提交表单时出现的错误消息:
Notice: Array to string conversion in
/home/username/public_html/phpmailer/class.phpmailer.php on line 2395
Could not access file: Array
SERVER -> CLIENT: 220-n1plcpnl0057.prod.ams1.secureserver.net ESMTP Exim 4.85
#2 Thu, 07 Jan 2016 07:46:23 -0700 220-We do not authorize the use of this
system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO mywebsite.com
SERVER -> CLIENT: 250-n1plcpnl0057.prod.ams1.secureserver.net Hello
n1plcpnl0057.prod.ams1.secureserver.net [46.252.205.183]250-SIZE 52428800250-
8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 TLS go ahead
CLIENT -> SERVER: EHLO mywebsite.com
SERVER -> CLIENT: 250-n1plcpnl0057.prod.ams1.secureserver.net Hello
n1plcpnl0057.prod.ams1.secureserver.net [46.252.205.183]250-SIZE 52428800250-
8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: IyMjIyMjIyMjIyMjIyMjIyMjIw==
SERVER -> CLIENT: 535 Incorrect authentication data
SMTP ERROR: Password command failed: 535 Incorrect authentication data
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 n1plcpnl0057.prod.ams1.secureserver.net closing
connection
SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Warning: Cannot modify header information - headers already sent by (output
started at /home/myusername/public_html/contact.php:4) in
/home/username/public_html/bootstrap.php on line 148
bootstrap.php第148行的代码是
header("Location: contact.php?status=thanks");
感谢您的帮助。
答案 0 :(得分:0)
您的问题是您将整个$_FILES['attachment']
数组传递给第一个参数中的addAttachment()
方法,而不是包含文件路径的字符串。因此错误信息!
此文件位置的路径位于$_FILES['attachment']['tmp_name']
,因为您尚未将上传的文件从tmp存储移动到永久文件位置。
如果您愿意,也可以传递参数2,也可以在$_FILES['attachment']['name']
所以像这样修改这段代码
//Add attachment
$mail->addAttachment($_FILES['attachment']['tmp_name'],
$_FILES['attachment']['name']);
该方法的原型是:
public function AddAttachment($path,
$name = '',
$encoding = 'base64',
$type = 'application/octet-stream')
答案 1 :(得分:0)
您是否尝试阅读错误消息?它包含指向故障排除指南的链接,其中包含GoDaddy以及如何连接到Gmail。您已经将代码基于一些随机的,过时的示例,而不是PHPMailer提供的维护代码。
在这种情况下,您告诉它连接到smtp.gmail.com
,但它显示您正在连接到n1plcpnl0057.prod.ams1.secureserver.net
,这是一个GoDaddy服务器 - 它们正在拦截并重定向您的SMTP流量 - 所以当然你的gmail凭证不起作用。
除此之外,您对addAttachment
的使用既错误又不安全,但并不是导致连接问题的原因。
顺便说一句,您的SMTP记录以易于解码的形式显示您的ID和密码,因此您应该更改它们。