php - php将上传的文件作为附件发送给电子邮件

  显示原文与译文双语对照的内容
51 2

我是网页设计新手。我需要用HTML创建一个表单,让用户输入几个字段并上传他/她的简历。当他提交表单时,他的提交应该是以电子邮件附件的形式给我发送的。我已经使用PHP发送电子邮件。除了文件没有被发送到发送的邮件之外,一切都正常。

我正在发布HTML和PHP代码,请帮助我。

HTML代码:文件名:Careers.html

 <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Contact Form</title>
</head>
<body>
<form action="Careers.php" method="post" enctype="multipart/form-data">
 Name<br>
 <input type="text" name="cf_name"><br>
 E-mail<br>
 <input type="text" name="cf_email"><br>
 Date of Birth<br>
 <input type="text" name="db_name"><br>
 Contact Number<br>
 <input type="text" name="db_cntct"><br>
 Class 12 Marks/CGPA/Percentage<br>
 <input type="text" name="cf_board"><br>
 Graduation Marks/CGPA/Percentage<br>
 <input type="text" name="db_grad"><br>
 Post-Graduation Marks/CGPA/Percentage<br>
 <input type="text" name="cf_pgrad"><br>
 Present Employer<br>
 <input type="text" name="db_emplyr"><br>
 Date of Joining<br>
 <input type="text" name="cf_doj"><br>
 Designation<br>
 <input type="text" name="db_desg"><br>
 Current CTC<br>
 <input type="text" name="db_ctc"><br>
 Upload your Resume<br>
 <input type="file" name="attachment" size="40"><br>
Message<br>
<textarea name="cf_message"></textarea><br><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
</body>
</html>

PHP代码:文件名:Careers.php

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_dob = $_POST['db_name'];
$field_contact = $_POST['db_cntct'];
$field_board = $_POST['cf_board'];
$field_grad = $_POST['db_grad'];
$field_pgrad = $_POST['cf_pgrad'];
$field_emplyr = $_POST['db_emplyr'];
$field_doj = $_POST['cf_doj'];
$field_desg = $_POST['db_desg'];
$field_ctc = $_POST['db_ctc'];
$field_message = $_POST['cf_message'];
$mail_to = 'sachinrocksus@gmail.com';
$subject = 'Job Application from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."n";
$body_message. = 'E-mail: '.$field_email."n";
$body_message. = 'Date of Birth: '.$field_dob."n";
$body_message. = 'Contact Number: '.$field_contact."n";
$body_message. = 'Class 12 Marks/CGPA/Percentage: '.$field_board."n";
$body_message. = 'Graduation Marks/CGPA/Percentage: '.$field_grad."n";
$body_message. = 'Post-Graduation Marks/CGPA/Percentage: '.$field_pgrad."n";
$body_message. = 'Present Employer: '.$field_emplyr."n";
$body_message. = 'Date of Joining: '.$field_doj."n";
$body_message. = 'Designation: '.$field_desg."n";
$body_message. = 'Current CTC: '.$field_ctc."n";
$body_message. = 'Message: '.$field_message."n";
$headers = 'From: '.$field_email."rn";
$headers. = 'Reply-To: '.$field_email."rn"; 
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {?>
 <script language="javascript" type="text/javascript">
 alert('Your Job Application has been recieved. We will contact you shortly.');
 window.location = 'Careers.html';
 </script>
<?php
}
else {?>
 <script language="javascript" type="text/javascript">
 alert('Message failed');
 window.location = 'Careers.html';
 </script>
<?php
}
?>
时间:原作者:0个回答

132 5

你没有把任何东西作为附件

我正在粘贴一个Fragment希望这有助于你

<?php
 $fileatt ="mypdffile.pdf";//Path to the file
 $fileatt_type ="application/pdf";//File Type
 $fileatt_name ="mypdffile.pdf";//Filename that will be used for the file as the attachment
 $email_from ="sales@mysite.com";//Who the email is from
 $email_subject ="Your attached file";//The Subject of the email
 $email_message ="Thanks for visiting mysite.com! Here is your free file.
";
 $email_message. ="Thanks for visiting.
";//Message that the email has in it
 $email_to = $_POST['email'];//Who the email is to
 $headers ="From:".$email_from;
 $file = fopen($fileatt,'rb');
 $data = fread($file,filesize($fileatt));
 fclose($file);
 $semi_rand = md5(time());
 $mime_boundary ="==Multipart_Boundary_x{$semi_rand}x";
 $headers. ="nMIME-Version: 1.0n". 
"Content-Type: multipart/mixed;n". 
" boundary="{$mime_boundary}"";
 $email_message. ="This is a multi-part message in MIME format.nn". 
"--{$mime_boundary}n". 
"Content-Type:text/html; charset="iso-8859-1"n". 
"Content-Transfer-Encoding: 7bitnn". 
 $email_message. ="nn";
 $data = chunk_split(base64_encode($data));
 $email_message. ="--{$mime_boundary}n". 
"Content-Type: {$fileatt_type};n". 
" name="{$fileatt_name}"n". 
//"Content-Disposition: attachment;n". 
//" filename="{$fileatt_name}"n". 
"Content-Transfer-Encoding: base64nn". 
 $data. ="nn". 
"--{$mime_boundary}--n";
 $ok = @mail($email_to, $email_subject, $email_message, $headers);
 if($ok) {
 echo"You file has been sent
 to the email address you specified.
 Make sure to check your junk mail!
 Click here to return to mysite.com.";
 } else {
 die("Sorry but the email could not be sent. Please go back and try again!");
 }
?> 
原作者:
125 3

这个函数发送上传文件作为 attachment.Please 参考教程的html代码,并逐步解释。

http://www.pearlbells.co.uk/send-upload-file-email-attachment-php/

function pepareAttachment( $filename, $fileorgname) {
 $attachContent = '';
 $file = fopen($filename,"rb");
 $data = fread($file,filesize($filename));
 fclose($file);
 $cvData = chunk_split(base64_encode($data));
 $attachContent. ="Content-Type: {"application/octet-stream"};n"." name="$fileorgname"n". 
"Content-Disposition: attachment;n"." filename="$fileorgname"n". 
"Content-Transfer-Encoding: base64nn". $cvData."nn";
 $attachContent. ="--{$mime_boundary}n"; 
 return $attachContent;
}
function sendMailAsAttachment( $filename, $fileorgname, $formData ) {
$emailData = prepareEmail( $formData );
$attachContent = prepareAttachment( $filename,$fileorgname );
$message = $emailData['message'].$attachContent;
$ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']); 
if ($ok) { 
 echo"<p>mail sent to $to!</p>"; 
} else { 
 echo"<p>mail could not be sent!</p>"; 
} 
}
原作者:
107 4

感谢你共享这样一段好代码:稍微修改一下这就是真正的工作代码。创建一个HTML文件:

上传文件并作为电子邮件附件发送

名称:
电子邮件:
选择要上载的文件:

同一个文件夹生成一个名为 mail_sender.php的php文件

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 $name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
 $formData = $_POST;
 getFile( $name_of_uploaded_file, $formData );
 function getFile( $filename, $formData ) {
 $allowedExts = array("csv","pdf","jpg","png","JPG","PNG","jpeg","JPEG");
 $temp = explode(".", $_FILES["uploaded_file"]["name"]);
 $extension = end($temp);
 $mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','application/jpg','image/jpg', 'image/jpeg', 'image/png','image/gif');
 if (in_array($_FILES['uploaded_file']['type'],$mimes )
 && ($_FILES["uploaded_file"]["size"] <2000000)
 && in_array($extension, $allowedExts))
 {
 if ($_FILES["uploaded_file"]["error"]> 0)
 {
 echo"Return Code:". $_FILES["uploaded_file"]["error"]."<br>";
 }
 else
 { 
 sendMailAsAttachment($_FILES["uploaded_file"]["tmp_name"],$_FILES["uploaded_file"]["name"],$formData); 
 }
 }
 else
 {
 echo"Invalid file". $extension." {}". $_FILES['uploaded_file']['type'];
//echo in_array($_FILES['uploaded_file']['type'],$mimes );
 } 
 }
//This function accepts post data on form submissions and prepare the email message from the form data.
 function prepareEmail( $formData ) {
//email fields: to, from, subject, and so on
 $to ="solimankhulna@gmail.com";
 $from ="solimankhulna@solimankhulna.com"; 
 $subject =""; 
 $message ="Uploaded Filen";
 $message. ="Name :". $formData['name']."n";
 $message. ="Email Address :". $formData['email']."n";
 $headers ="From: $from";
//boundary 
 $semi_rand = md5(time()); 
 $mime_boundary ="==Multipart_Boundary_x{$semi_rand}x"; 
//headers for attachment 
 $headers. ="nMIME-Version: 1.0n"."Content-Type: multipart/mixed;n"." boundary="{$mime_boundary}""; 
//multipart boundary 
 $message. ="This is a multi-part message in MIME format.nn"."--{$mime_boundary}n"."Content-Type: text/plain; charset="iso-8859-1"n"."Content-Transfer-Encoding: 7bitnn". $message."nn"; 
 $message. ="--{$mime_boundary}n";
 $emailData = array (
 'to' => $to,
 'from' => $from,
 'subject' => $subject,
 'headers' => $headers,
 'message' => $message
 );
 return $emailData;
 }
 function prepareAttachment( $filename, $fileorgname) {
 $semi_rand = md5(time()); 
 $mime_boundary ="==Multipart_Boundary_x{$semi_rand}x";
 $attachContent = '';
 $file = fopen($filename,"rb");
 $data = fread($file,filesize($filename));
 fclose($file);
 $cvData = chunk_split(base64_encode($data));
 $attachContent. ="Content-Type: {"application/octet-stream"};n"." name="$fileorgname"n". 
"Content-Disposition: attachment;n"." filename="$fileorgname"n". 
"Content-Transfer-Encoding: base64nn". $cvData."nn";
 $attachContent. ="--{$mime_boundary}n"; 
 return $attachContent;
 }
 function sendMailAsAttachment( $filename, $fileorgname, $formData ) {
 $emailData = prepareEmail( $formData );
 $attachContent = prepareAttachment( $filename,$fileorgname );
 $message = $emailData['message'].$attachContent;
 $ok = @mail($emailData['to'], $emailData['subject'], $message, $emailData['headers']); 
 if ($ok) { 
 echo"<p>mail sent to". $emailData['to']."!</p>"; 
 } else { 
 echo"<p>mail could not be sent!</p>"; 
 } 
 }
?>
原作者:
73 2

如 above 所述,你没有获得 PHP 部件中处理的任何附件。这个教程可以帮助你逐步浏览各个步骤。

原作者:
...