【问题标题】:attachment is not working in mail function wordpress附件在邮件功能 wordpress 中不起作用
【发布时间】:2016-10-20 05:26:22
【问题描述】:

短信可以正常工作,但我也想附加文件。 附件不起作用。 我的代码是:

 <form method="post" enctype="multipart/form-data">
     <input name="filepc" type="file" id="filepc" class="listEm" />
 </form>


if (isset($_POST['submit'])) {
  $attachments = '';
    if (!empty($_FILES['filepc']['tmp_name'])) {
        $attachments = array($_FILES['filepc']['name']);

    }

headers = "From:" . $your_email . " < " . $email . ">" . "\r\n";
        $headers .= "Reply - To:" . $ct_email . "\r\n";
        $headers .= "Content-Disposition: attachment; filename=\"".$attachments."\"\r\n\r\n";
        //$headers = "Bcc: someone@domain . com" . "\r\n";
        $headers = "X - Mailer: PHP / " . phpversion();
        mail($to , $subject , $msg , $headers,$attachments);
$successMsg = '<h5 style="color:red;">Sent successfully</h5>';
//        }

}

以上代码有效,但未附加文件。请帮我找到这个问题的解决方案。

【问题讨论】:

    标签: php wordpress email


    【解决方案1】:
    if(!empty($_FILES['resume']['name'])) {
        $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
        $tmp_name = $_FILES['resume']['tmp_name'];
        $type = $_FILES['resume']['type'];
        $file_name = $_FILES['resume']['name'];
        $size = $_FILES['resume']['size'];
    
        // Check to make sure that it is an uploaded file and not a system file
        if(is_uploaded_file($tmp_name)){
            // Now Open the file for a binary read
            $file = fopen($tmp_name,'rb');
            // Now read the file content into a variable
            $data = fread($file,filesize($tmp_name));
            // close the file
            fclose($file);
            // Now we need to encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
        }
        $mybody = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
        $mybody . "\n\n";
    
        $headers = "From: $from\r\n" .
             "MIME-Version: 1.0\r\n" .
             "Content-Type: multipart/mixed;\r\n" .
             " boundary=\"{$mime_boundary}\"";
    
        $mybody .= "--{$mime_boundary}\n" .
             "Content-Type: {$type};\n" .
             " name=\"{$file_name}\"\n" .
             //"Content-Disposition: attachment;\n" .
             //" filename=\"{$fileatt_name}\"\n" .
             "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" .
            "--{$mime_boundary}--\n";
    
        $bodys .= "$mybody <br>";
        $subject = "Attachment";
        $body = $body . $bodys;
        mail($to, $subject, $body, $headers);
    }
    

    【讨论】:

    • $msg = "\n\n" 。 “电话:$title”。 “\n”。 “名字:$first_name”。 “\n”。 “姓氏:$last_name”。 “\n”。 “附件:回声'图像:';" . “\n”。 “首选联系方式:$prmode”。 "\n";如何在带有附件的电子邮件中的消息中添加此内容
    • 如何处理多个附件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2011-04-07
    • 2012-07-12
    相关资源
    最近更新 更多