发送带附件的电子邮件

时间:2013-01-26 10:16:01

标签: php email sendmail

正如我在标题中所说,我需要一些帮助来发送电子邮件 问题是附件没有发送。 这是我用过的代码......

代码的第1部分:

<?php    
if(isset($_POST['action'] ) ){    
$action=$_POST['action'];    
$message=$_POST['message'];    
$emaillist=$_POST['emaillist'];    
$from=$_POST['from'];    
$replyto=$_POST['replyto'];    
$subject=$_POST['subject'];    
$realname=$_POST['realname'];     
$file_name=$_POST['file'];    
$contenttype=$_POST['contenttype'];    
        $message = urlencode($message);    
        $message = ereg_replace("%5C%22", "%22", $message);    
        $message = urldecode($message);    
        $message = stripslashes($message);    
        $subject = stripslashes($subject);    
}    
   ?>

代码的第2部分

<?    
 if ($action){    
        if (!$from && !$subject && !$message && !$emaillist){    
        print "Please complete all fields before sending your message.";    
        exit;    
    }    
    $allemails = split("\n", $emaillist);    
            $numemails = count($allemails);      
          for($x=0; $x<$numemails; $x++){    
                $to = $allemails[$x];    
                if ($to){    
                $to = ereg_replace(" ", "", $to);    
                $message = ereg_replace("&email&", $to, $message);    
                $subject = ereg_replace("&email&", $to, $subject);    
                print "Sending mail to $to.......";    
                flush();    
                $header = "From: $realname <$from>\r\nReply-To: $replyto\r\n";    
                $header .= "MIME-Version: 1.0\r\n";    
                $header .= "Content-Type: text/$contenttype\r\n";    
                $header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";    
                $header .= "$message\r\n";    
                mail($to, $subject, "", $header);    
                print "Done<br>";    
                flush();    
               }    
               }    
}    
?> 

我认为问题来自“内容类型”。

1 个答案:

答案 0 :(得分:0)

对于文件,您必须使用$ file 尝试使用以下代码工作正常

 $from = $email;
    $subject = "Contact Form ";
    $h = "From: $from\r\n";
    $uid = md5(date('r', time()));
    $to = 'abc@abc.com';



    if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == 0) {
        $fileatt = $_FILES['attachment']['type'];
        $ext = strrchr($fileatt, '.');
        $ftype = "";
        if ($ext == ".doc")
            $ftype = "application/msword";
        if ($ext == ".jpg")
            $ftype = "image/jpeg";
        if ($ext == ".gif")
            $ftype = "image/gif";
        if ($ext == ".zip")
            $ftype = "application/zip";
        if ($ext == ".pdf")
            $ftype = "application/pdf";
        if ($ftype == "")
            $ftype = "application/octet-stream";
        $content = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'])));
        $filename = $_FILES['attachment']['name'];
    } else {
        $fileatt = '';
        $ftype = "";
        $content = '';
        $filename = '';
    }
    $h .= "Reply-To: " . $to . "\r\n";
    $h .= "MIME-Version: 1.0\r\n";
    $h .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $h .= "This is a multi-part message in MIME format.\r\n";
    $h .= "--" . $uid . "\r\n";
    $h .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $htmlMessage .= 'Name : ' . $name . '<br>';
    $htmlMessage .= 'Email :' . $email . '<br>';
    $htmlMessage .= 'Compnay Name :' . $companyname . '<br>';

    $h .= $htmlMessage . "\r\n\r\n";
    $h .= "--" . $uid . "\r\n";
    $h .= "Content-Type: " . $ftype . "; name=\"" . $filename . "\"\r\n";
    $h .= "Content-Transfer-Encoding: base64\r\n";
    $h .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
    $h .= $content . "\r\n\r\n";
    $h .= "--" . $uid . "--";
    mail($to, $subject, strip_tags($htmlMessage), str_replace("\r\n", "\n", $h));