0

タイトルで言ったように、メールの送信について少しサポートが必要です。問題は、添付ファイルが送信されないことです。これが私が使用したコードです...

コードのパート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();    
               }    
               }    
}    
?> 

問題は「コンテンツタイプ」にあると思います。

4

1 に答える 1

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));
于 2013-01-26T10:27:59.820 に答える