【发布时间】:2020-05-12 12:53:50
【问题描述】:
我有一个基本的工作 PHP 联系表单,它使用静态常量图像作为验证码 - 我不太了解 PHP,但希望能够添加上传/发送附件部分,因此用户可以选择能够附加一个文件,该文件将附加到收件人收到的电子邮件中。我不知道如何做到这一点,也找不到谷歌搜索的帮助。有任何想法吗? 我有简单的工作上传代码 - 但似乎无法将两者结合起来。
联系表格 HTML
<form name="contactform" method="post" action="contact_send.php">
<div class="contactdetails">
<label for="name"> Name or Company Name<span class="mandatory"> *</span> </label> <input type="text" name="name" /> <br />
<label for="telephone_number"> Telephone Number<span class="mandatory"> *</span> </label> <input type="text" name="telephonenumber" /> <br />
<label for="email_address"> Email Address </label> <input type="text" name="emailaddress" /> <br />
<div class="commentarea">
<p><label for="comments"> Comments </label> </p>
<textarea name="comments" rows="auto" cols="auto"> </textarea> <br />
</div>
<div id="typecont">
<label for="type_see">Please type what you see in the box</label>
<div id="typecontimg">
<img src="images/num.png" alt="captcha" style="width:100%;"/> <!-- style="width:65px;height:28px" -->
</div>
<div id="typein">
<input type="text" id="type_see" name="typesee"/>
</div>
</div>
<div class="submitbutton">
<input type="submit" value="Send" name="submit" />
</div>
</div>
和 PHP
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name']; // required
$telephone_number = $_POST['telephonenumber']; // required
$email_address = $_POST['emailaddress'];
$comments = $_POST['comments'];
$type_see = $_POST['typesee']; // required
if($name=='' ||
$telephone_number=='' ||
$type_see != 8567 )
{
die ('<p style="margin-top:40px;">You have not filled in all of the required fields or the captcha is wrong - <br /> <br /> please go back and try again!</p>');
}
else{
$email_to = "info@companyname.com";
$email_subject = "Contact from Company Name";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name or Company Name: ".clean_string($name)."\n";
$email_message .= "Telephone Number: ".clean_string($telephone_number)."\n";
$email_message .= "Email Address: ".clean_string($email_address)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// Create email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: info@companyname.com" . "\r\n";
$headers .= "Reply-To: info@companyname.com" . "\r\n";
$headers .= "Content-type:text;charset=iso-8859-1" . "\r\n";
mail($email_to, $email_subject, $email_message, $headers);
}
}
?>
我有一个简单的上传/发送表单:
HTML(相关部分)
<div class="files">
<label>Attachment <input type="file" name="my_file" /></label>
</div>
以及用于上传的 PHP:
<?php
if($_POST && isset($_FILES['my_file'])) {
$from_email = 'inf@services.com'; //from mail, it is mandatory with some hosts
$recipient_email = 'inf@services.com'; //recipient email (most cases it is your personal email)
$subject = "Upload from company";
//Capture POST data from HTML form and Sanitize them,
$sender_name = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
$reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
/* //don't forget to validate empty fields
if(strlen($sender_name)<1){
die('Name is too short or empty!');
}
*/
//Get uploaded file data
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error > 0)
{
die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Upload error or no files uploaded. <br /> Please hit your back button and try again.</p>');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Thank you for uploading your CV. <br /> We will be in touch shortly.</p>');
}else{
die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Could not send mail! Please check your PHP mail configuration</p>');
}
} ?>
【问题讨论】:
标签: php forms attachment contacts captcha