【发布时间】:2019-02-05 07:36:32
【问题描述】:
我是 php 编码的新手。 我的网站上有一个联系表格,允许用户将文件作为附件发送到电子邮件。
根据互联网上的参考资料,我终于通过联系表格发送了电子邮件。所以问题是文件上传到服务器但邮件到达时没有任何附件。
我创建了名为upload 的文件夹,文件只放在那里。 我是不是错过了什么。以下是我正在使用的代码:
HTML:
<form action="send_form_email.php" method="post" enctype="multipart/form-data" name="servicesform" id="servicesform" autocomplete="on">
<div class="control-group form-group">
<div class="controls">
<label class="contact-p1">Full Name:</label>
<input type="text" class="form-control" name="yourname" id="yourname" required data-validation-required-message="Please enter your name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label class="contact-p1">Phone Number:</label>
<input type="tel" class="form-control" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label class="contact-p1">Email Address:</label>
<input type="email" class="form-control" name="email" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block"></p>
</div>
</div>
<div class="form-inputs">
<p>Specify yourself</p>
<select name="enquiring" required data-validation-required-message="Please select your field." >
<option value="" > -- Please select -- </option>
<option>Employer</option>
<option>Employed</option>
<option>Unemployed</option>
<option>Student</option>
</select>
<div class="clearfix"></div>
</div>
<div class="form-inputs">
<p>Choose your field</p>
<select name="field" required data-validation-required-message="Please select your field." >
<option value="" > -- Please select -- </option>
<option>Banking</option>
<option>Finance</option>
<option>It</option>
<option>Specialists</option>
</select>
<div class="clearfix"></div>
</div>
<div class="form-inputs upload">
<p>Upload your resume</p>
<input type="file" id="uploads" name="uploads" multiple>
<div id="filedrag">Upload</div>
</div>
<!-- For success/fail messages -->
<button type="reset" name="reset" class="btn btn-primary">Reset</button>
<button type="submit" class="btn btn-primary">Send</button>
</form>
PHP:
<?php
if(isset($_POST['email'])) {
// TO AND FROM
$email_to = "info@mysite.com";
$email_subject = "Enquiry from my website";
function died($error) {
// ERROR MESSAGES TO THE USER
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// VALIDATION ON EXPECTED DATA
if(!isset($_POST['yourname']) ||
!isset($_POST['phone']) ||
!isset($_POST['email']) ||
!isset($_POST['enquiring']) ||
!isset($_POST['field'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name_from = $_POST['yourname']; // required
$phone = $_POST['phone']; // not required
$email_from = $_POST['email']; // required
$enquiring = $_POST['enquiring']; // not required
$field = $_POST['field']; // not required
$uploads = $_FILES['uploads']; // not required
// MANDATORY FIELDS
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The email address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name_from)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Services Form.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "I am ".clean_string($enquiring)."\n";
$email_message .= "field: ".clean_string($field)."\n";
// FILE UPLOADS
$allowedExts = array("ai", "doc", "docx", "gif", "jpeg", "jpg", "pdf", "png", "psd");
$extension = end(explode(".", $_FILES["uploads"]["name"]));
if ((($_FILES["uploads"]["type"] == "image/gif")
|| ($_FILES["uploads"]["type"] == "image/jpeg")
|| ($_FILES["uploads"]["type"] == "image/png")
|| ($_FILES["uploads"]["type"] == "image/pjpeg"))
|| ($_FILES["uploads"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["uploads"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
echo "Upload: " . $_FILES["uploads"]["name"] . "<br />";
echo "Type: " . $_FILES["uploads"]["type"] . "<br />";
echo "Size: " . ($_FILES["uploads"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["uploads"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["uploads"]["name"])) {
echo $_FILES["uploads"]["name"] . " already exists. ";
}
else {
move_uploaded_file($_FILES["uploads"]["tmp_name"],
"upload/" . $_FILES["uploads"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["uploads"]["name"];
}
}
}
else {
echo "Invalid file";
}
// EMAIL HEADERS
$headers .= "From:".$from_email."\r\n"; // Sender Email
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- SUCCESS MESSAGE -->
<?php
header("Location: thank-you.html");
exit();
?>
Your message has been sent. Thank you for contacting us, We'll get back to you as soon as possible.
<?php
}
?>
【问题讨论】:
-
不要使用原始的 mail() 函数,使用 PHPMailer 或 SwiftMailer 或其他一些库。不要使用正则表达式来检查电子邮件,使用带有 FILTER_VALIDATE_EMAIL 过滤器的 filter_var() 函数。你的名字正则表达式也不好,例如我的名字包含字符'ý'和'á',你的过滤器会发现它无效。
-
mail() 函数是唯一的要求。不能使用 PHPMailer 或 Swiftmailer。你能指出这段代码中的问题并纠正它吗?
-
嗯,这不是一个好的要求。请问是什么原因?有空的时候我会尝试修复你的代码。
-
明显的错误是您没有将附件附加到您的电子邮件中。