通过附件发送电子邮件

时间:2013-05-14 10:33:50

标签: php email

尝试使用PHP完成此操作,我很困难,电子邮件中的HTML很好,但boundary部分显示为普通文本,附件不会附加。

$to = 'myemail@email.com';
$subject = 'Contact Submission';
$name       =   strip_tags($_POST['name']);
$emailto    =   strip_tags($_POST['emailto']);
$comments   =   strip_tags($_POST['comments']);
$attachment =   chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename   =   $_FILES['file']['name'];

$boundary =md5(date('r', time())); 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: noreply@emailcom' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

$comments="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/html; charset=ISO-8859-1\"
Content-Transfer-Encoding: 7bit

        <table width=\"600px\" cellpadding=\"0\" cellspacing=\"0\"style=\"\">
          <tr valign=\"top\">
            <th align=\"left\" style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:14px; padding-left:10px; line-height:32px\">Contact Submission</th>
        </tr>
        </table>
        <br />

        <table width=\"600\" cellpadding=\"0\" cellspacing=\"0\"style=\"border: 1px solid #000;\">
          <tr valign=\"top\">
            <td colspan=\"4\" style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; border-bottom: 1px solid #000;background:#ececec; line-height:32px;\" align=\"left\"><strong>Contact Information</strong></td>
        </tr>
          <tr valign=\"top\">
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold\" align=\"right\" width=\"120\">Name</td>
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;padding-left:40px;\">$name</td>    
        </tr>
          <tr valign=\"top\">
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold; background:#e6edf2;\" align=\"right\" width=\"120\">Email</td>
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;background:#e6edf2;padding-left:40px;\">$emailto</td>
          </tr>
          <tr valign=\"top\">
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold\" align=\"right\">Comments</td>
            <td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;padding-left:40px;\" colspan=\"3\">$comments</td>
          </tr>
        </table>

        --_2_$boundary--
        --_1_$boundary
        Content-Type: application/octet-stream; name=\"$filename\" 
        Content-Transfer-Encoding: base64 
        Content-Disposition: attachment 

        $attachment
        --_1_$boundary--";

                mail($to, $subject, $comments, $headers);

对于任何需要更改/添加/删除的内容,以获取此电子邮件以保留我的HTML表格并附加文件

,任何人都可以获得任何帮助。

1 个答案:

答案 0 :(得分:2)

$to = "email";
                    $subject = "your subject";
                    $base = basename($file1);
$file = fopen('file path','rb');
$size = filesize('file path');
$data = fread($file,$size);
fclose($file);
$data = chunk_split(base64_encode($data));
$message="<html><body>";
$message="<table border='1'>";
$message.="<tr><td colspan='3' align='center' style='color:#FFFFFF;font-size:large; background:#000000'>Your Information</td></tr>";
$message.="<tr><td style='font-weight:bold'>field 1</td><td style='font-weight:bold'>field 2</td></tr>";
$message.="</table>";
$message.="</body></html>";

//boundary
$div = "==Multipart_Boundary_x".md5(time())."x";
//headers
$head = "From: $email\n".
    "MIME-Version: 1.0\n".
    "Content-Type: multipart/mixed;\n".
    " boundary=\"$div\"";
//message
$mess = "--$div\n".
    "Content-Type: text/html; charset=\"iso-8859-1\"\n".
    "Content-Transfer-Encoding: 7bit\n\n".
    "$message\n\n".
    "--$div\n".
    "Content-Type: application/octet-stream; name=\"$base\"\n".
    "Content-Description: $base\n".
    "Content-Disposition: attachment;\n".
    " filename=\"$base\"; size=$size;\n".
    "Content-Transfer-Encoding: base64\n\n".
    "$data\n\n".
    "--$div\n";
$return = "-f$email";

mail($to,$subject,$mess,$head,$return);

试试这段代码