需要带有文件附件脚本的 PHP 邮件程序表单来发送带或不带附件的表单信息
我在尝试让我的脚本发送包含从表单收集的信息的文件附件时遇到一些麻烦。我使用 带文件附件的 PHP 邮件程序表单 并进行修改以符合我的规范。到目前为止,它将发送不带附加图像的表单信息,但是当我上传图像时,它只会发送附件,而不会发送表单中的其他信息。我需要它能够发送带或不带附件的表单信息。我是 php 的初学者,所以我不确定为什么要这样做。
这是我的代码
<?php
if(isset($_POST['submit'])){
$sendTo = '<[email protected]';
$subject = 'Request';
$from = $_POST['email_address'];
// Allowed file types. add file extensions WITHOUT the dot.
$allowtypes=array("jpg", "png", "gif");
// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
$requirefile="true";
// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
$max_file_size="5120";
$errors = array(); //Initialize error array
//Check if the file type uploaded is a valid file type.
if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
// basename -- Returns filename component of path
$filename = basename($_FILES['attachment']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
$filesize=$_FILES['attachment']['size'];
$max_bytes=$max_file_size*5120;
//Check if the file type uploaded is a valid file type.
if (!in_array($ext, $allowtypes)) {
$errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
// check the size of each file
} elseif($filesize > $max_bytes) {
$errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
}
} // if !empty FILES
// send an email
// Obtain file upload vars
$fileatt = $_FILES['attachment']['tmp_name'];
$fileatt_type = $_FILES['attachment']['type'];
$fileatt_name = $_FILES['attachment']['name'];
// Headers
$headers = 'From: '.$from = $_POST['email_address'];
// create a boundary string. It must be unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message ="This is a multi-part message in MIME format.\n\n";
$message.="--{$mime_boundary}\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: 7bit\n\n";
$message .= "Name: \t\t".$_POST['name']."\n";
$message .= "Email: \t\t".$_POST['email_address']."\n";
if($_POST['phone_number'] != ""){
$message .= "Phone: \t\t".$_POST['phone']."\n";
$message .= "Date: \t\t".$_POST['full_date']."\n";
}
$message .= "\n";
if($_POST['additional_info'] != ""){
$message .= "Additional Information: \n".$_POST['additional_info']."\n";
}
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the completed message
mail($sendTo, $subject, $message, $headers);
header("Location: complete.php");
}
?>
I'm having some trouble trying to get my script to send a file attachment with the information collected from the form. I used PHP Mailer Form with File Attachment and modified to fit my specification. So far it will send the form information without the attached image but when I upload an image it will only send the attachment and no other information from the form. I need it to be able to send the form information with or without an attachment. I'm a beginner with php so I'm not sure why its doing this.
Here is my code
<?php
if(isset($_POST['submit'])){
$sendTo = '<[email protected]';
$subject = 'Request';
$from = $_POST['email_address'];
// Allowed file types. add file extensions WITHOUT the dot.
$allowtypes=array("jpg", "png", "gif");
// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
$requirefile="true";
// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
$max_file_size="5120";
$errors = array(); //Initialize error array
//Check if the file type uploaded is a valid file type.
if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
// basename -- Returns filename component of path
$filename = basename($_FILES['attachment']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
$filesize=$_FILES['attachment']['size'];
$max_bytes=$max_file_size*5120;
//Check if the file type uploaded is a valid file type.
if (!in_array($ext, $allowtypes)) {
$errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
// check the size of each file
} elseif($filesize > $max_bytes) {
$errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
}
} // if !empty FILES
// send an email
// Obtain file upload vars
$fileatt = $_FILES['attachment']['tmp_name'];
$fileatt_type = $_FILES['attachment']['type'];
$fileatt_name = $_FILES['attachment']['name'];
// Headers
$headers = 'From: '.$from = $_POST['email_address'];
// create a boundary string. It must be unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message ="This is a multi-part message in MIME format.\n\n";
$message.="--{$mime_boundary}\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: 7bit\n\n";
$message .= "Name: \t\t".$_POST['name']."\n";
$message .= "Email: \t\t".$_POST['email_address']."\n";
if($_POST['phone_number'] != ""){
$message .= "Phone: \t\t".$_POST['phone']."\n";
$message .= "Date: \t\t".$_POST['full_date']."\n";
}
$message .= "\n";
if($_POST['additional_info'] != ""){
$message .= "Additional Information: \n".$_POST['additional_info']."\n";
}
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the completed message
mail($sendTo, $subject, $message, $headers);
header("Location: complete.php");
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我发送带有附件的邮件所做的事情。部分代码是我的,部分代码取自 http://php.net /manual/en/function.mail.php。这段代码已经过尝试和测试,所以它应该可以工作。另请查看是否安装了 sendmail。如果在 linux ubuntu 系统上尝试 sudo apt-get install sendmail 来安装它。
文件名:index.php
顺便说一句,我对使用 sendmail 表示歉意,因为它有点慢。我会尝试发布更好的解决方案。
Here is what I have done to send a mail with an attachment. Some of the code is mine and some of it has been taken from http://php.net/manual/en/function.mail.php. This code has been tried and tested so, it should work. Also see if sendmail is installed. If on linux ubuntu system try sudo apt-get install sendmail to install it.
File Name: index.php
By the way I would like to apologize for using sendmail since it is a bit slow. I will try to post a better solution.