PHP:发送带有上传图像的文本邮件作为附件

时间:2014-08-31 13:02:12

标签: php forms email attachment

我真的需要以下脚本的帮助。我有一个上传表单,我希望上传的文件作为电子邮件附件发送。我在网上找到了一个代码段,但是当我发送邮件时,我的常规文本会被发送,并且附件会作为神秘字符粘贴到电子邮件中。

if ( isset($_POST['submit']) ):

        /* GET File Variables */ 
        $tmpName = $_FILES['attachment']['tmp_name']; 
        $fileType = $_FILES['attachment']['type']; 
        $fileName = $_FILES['attachment']['name']; 

        if ( file($tmpName) ) { 
          /* Reading file ('rb' = read binary)  */
          $file = fopen($tmpName,'rb'); 
          $data = fread($file,filesize($tmpName)); 
          fclose($file); 

          /* a boundary string */
          $randomVal = md5(time()); 
          $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

          /* Header for File Attachment */
          $headers .= "\nMIME-Version: 1.0\n"; 
          $headers .= "Content-Type: multipart/mixed;\n" ;
          $headers .= " boundary=\"{$mimeBoundary}\""; 

          /* Multipart Boundary above message */
          $message = "This is a multi-part message in MIME format.\n\n" . 
          "--{$mimeBoundary}\n" . 
          "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
          "Content-Transfer-Encoding: 7bit\n\n" . 
          $message . "\n\n"; 

          /* Encoding file data */
          $data = chunk_split(base64_encode($data)); 

          /* Adding attchment-file to message*/
          $message .= "--{$mimeBoundary}\n" . 
          "Content-Type: {$fileType};\n" . 
          " name=\"{$fileName}\"\n" . 
          "Content-Transfer-Encoding: base64\n\n" . 
          $data . "\n\n" . 
          "--{$mimeBoundary}--\n"; 
        } 

        $headers = "From: error@interreg.dev" . "\r\n";
        $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
        $headers .= "Reply-To: " . $mail . " \r\n";

        $subject = "Errormessage: " . $error;

        $message .= "this is some text that get's sent properly!";

        // Send Error
        $success = mail( $mail, $subject, $message, $headers ); ?>

知道我在这里做错了什么吗?我很乐意得到一些帮助。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

Please try this code  
<?php
  function send_mail_attachment($filename, $path, $mailto, $from_mail,    $from_name,$replyto, $subject, $message) 
{
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
    echo "mail send ... OK"; // or use booleans here
} else {
    echo "mail send ... ERROR!";
}
}
$my_file = "mysomefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "myscript";
$my_mail = "my@gmail.com";
$my_replyto = "myto@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hello are you like this script? I hope it will help";
$mailto="hello@gmail.com";
mail_attachment($my_file, $my_path,$mailto, $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?> 

答案 1 :(得分:0)

please try this code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#999999; text-align:right; vertical-align:top;}
input{ width:181px;}
</style>
</head>
<body>
<form action="emailSend.php" method="post" name="mainform" enctype="multipart/form-         data">
<table width="500" border="0" cellpadding="5" cellspacing="5">
   <tr>
    <th>Your Name</th>
    <td><input name="fieldFormName" type="text"></td>
</tr>
<tr>
<tr>
    <th>Your Email</th>
    <td><input name="fieldFormEmail" type="text"></td>
</tr>
<tr>
    <th>To Email</th>
    <td><input name="toEmail" type="text"></td>
</tr>

<tr>
    <th>Subject</th>
    <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
</tr>
<tr>
    <th>Comments</th>
    <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
</tr>
<tr>
  <th>Attach Your File</th>
  <td><input name="attachment" type="file"></td>
</tr>
<tr>
    <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>

下面是在电子邮件附件中发送该文件的PHP代码

<?php

$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName']; 
$subject = $_POST['fieldSubject']; 
$message = $_POST['fieldDescription'];

/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
/* Reading file ('rb' = read binary)  */
$file = fopen($tmpName,'rb'); 
$data = fread($file,filesize($tmpName)); 
fclose($file); 

 /* a boundary string */
 $randomVal = md5(time()); 
 $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

 /* Header for File Attachment */
 $headers .= "\nMIME-Version: 1.0\n"; 
 $headers .= "Content-Type: multipart/mixed;\n" ;
 $headers .= " boundary=\"{$mimeBoundary}\""; 

 /* Multipart Boundary above message */
 $message = "This is a multi-part message in MIME format.\n\n" . 
 "--{$mimeBoundary}\n" . 
 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
 "Content-Transfer-Encoding: 7bit\n\n" . 
 $message . "\n\n"; 

 /* Encoding file data */
 $data = chunk_split(base64_encode($data)); 

 /* Adding attchment-file to message*/
 $message .= "--{$mimeBoundary}\n" . 
 "Content-Type: {$fileType};\n" . 
 " name=\"{$fileName}\"\n" . 
 "Content-Transfer-Encoding: base64\n\n" . 
 $data . "\n\n" . 
 "--{$mimeBoundary}--\n"; 
 } 

 $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

 if($flgchk){
 echo "A email has been sent to: $to";
 }
 else
 {
 echo "Error in Email sending";
 }
 ?>