从计算机附加文件时无法使用wp_mail接收任何文件

时间:2021-11-24 15:17:20

I am receiving the attachment with email but it is just a file with no extension. (Attachment file is "File" and no extension). Here is my code.

我收到了附件和电子邮件,但它只是一个没有扩展名的文件。 (附件文件是“文件”,没有扩展名)。这是我的代码。

    $to = $user->user_email;
    $subject = $_POST['subject'];
    $body = $_POST['description'];
    $file = $_FILES["attachment_news"]["tmp_name"];
    $content = parse_url($file);
    $files = $content[path];
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $body, $headers, $files );

Here is the HTML

这是HTML

       <form method="post" action="<?php echo admin_url( 'admin-post.php' ) ?>"  enctype="multipart/form-data">
          <input type="hidden" name="action" value="newsletter_form">
          <label for="subject">Subject:</label>
          <input type="text" name="subject" id="subject">
          <label for="description">Description:</label>
          <input type="text" name="description" id="description">
          <label for="attachment_news">Upload file:</label>
          <input type="file" name="attachment_news" id="attachment_news">
          <input type="submit" name="submit" value="Submit">
        </form>

3 个解决方案

#1


0  

You have to pass multipart/mixed MIME type for attachment so use below code to pass headers.

您必须为附件传递multipart / mixed MIME类型,因此请使用下面的代码来传递标头。

$uid = md5(uniqid(time()));
$to = $user->user_email;
$subject = $_POST['subject'];
$body = $_POST['description'];
$file = $_FILES["attachment_news"]["tmp_name"];
$content = parse_url($file);
$files = $content[path];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
wp_mail( $to, $subject, $body, $headers, $files );

#2


0  

You should also add file name

您还应该添加文件名

   $to = $user->user_email;
    $subject = $_POST['subject'];
    $body = $_POST['description'];
    $file = $_FILES["attachment_news"]["tmp_name"];
    $content = parse_url($file);
    $files = array($content[path]);
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $body, $headers, $files );

#3


0  

Thank everyone for your participation in helping me out. I had to use mail instead of wp_mail to perform the desired task. I used below code which works perfectly.

感谢大家参与帮助我。我不得不使用邮件而不是wp_mail来执行所需的任务。我使用下面的代码完美地工作。

    $email = $user->user_email;
    $from = "info@domain.com"; 
    $subject = $_POST['subject'];
    $message = $_POST['description'];

    // Obtain file upload vars 
    $attachment_news = $_FILES['attachment_news']['tmp_name']; 
    $fileatt_type = $_FILES['attachment_news']['type']; 
    $fileatt_name = $_FILES['attachment_news']['name']; 
    //print_r($fileatt);
     //echo $fileatt_type."test".$fileatt_name; die;
    $headers = "From: $from"; 

    if (file($attachment_news)) { 
    // Read the file to be attached ('rb' = read binary) 
    $file = fopen($attachment_news,'rb'); 
    $data = fread($file,filesize($attachment_news)); 
    fclose($file); 

    // Generate a boundary string 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // Add the headers for a file attachment 
    $headers .= "\nMIME-Version: 1.0\n" . 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$mime_boundary}\""; 

    // Add a multipart boundary above the plain message 
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mime_boundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n"; 

    // Base64 encode the file data 
    $data = chunk_split(base64_encode($data)); 

    // Add file attachment to the message 
    $message .= "--{$mime_boundary}\n" . 
    "Content-Type: {$fileatt_type};\n" . 
    " name=\"{$fileatt_name}\"\n" . 
    //"Content-Disposition: attachment;\n" . 
    //" filename=\"{$fileatt_name}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mime_boundary}--\n"; 
    } 

    mail ("$email", "$subject", "$message", "$headers"); 

#1


0  

You have to pass multipart/mixed MIME type for attachment so use below code to pass headers.

您必须为附件传递multipart / mixed MIME类型,因此请使用下面的代码来传递标头。

$uid = md5(uniqid(time()));
$to = $user->user_email;
$subject = $_POST['subject'];
$body = $_POST['description'];
$file = $_FILES["attachment_news"]["tmp_name"];
$content = parse_url($file);
$files = $content[path];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
wp_mail( $to, $subject, $body, $headers, $files );

#2


0  

You should also add file name

您还应该添加文件名

   $to = $user->user_email;
    $subject = $_POST['subject'];
    $body = $_POST['description'];
    $file = $_FILES["attachment_news"]["tmp_name"];
    $content = parse_url($file);
    $files = array($content[path]);
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $body, $headers, $files );

#3


0  

Thank everyone for your participation in helping me out. I had to use mail instead of wp_mail to perform the desired task. I used below code which works perfectly.

感谢大家参与帮助我。我不得不使用邮件而不是wp_mail来执行所需的任务。我使用下面的代码完美地工作。

    $email = $user->user_email;
    $from = "info@domain.com"; 
    $subject = $_POST['subject'];
    $message = $_POST['description'];

    // Obtain file upload vars 
    $attachment_news = $_FILES['attachment_news']['tmp_name']; 
    $fileatt_type = $_FILES['attachment_news']['type']; 
    $fileatt_name = $_FILES['attachment_news']['name']; 
    //print_r($fileatt);
     //echo $fileatt_type."test".$fileatt_name; die;
    $headers = "From: $from"; 

    if (file($attachment_news)) { 
    // Read the file to be attached ('rb' = read binary) 
    $file = fopen($attachment_news,'rb'); 
    $data = fread($file,filesize($attachment_news)); 
    fclose($file); 

    // Generate a boundary string 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // Add the headers for a file attachment 
    $headers .= "\nMIME-Version: 1.0\n" . 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$mime_boundary}\""; 

    // Add a multipart boundary above the plain message 
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mime_boundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n"; 

    // Base64 encode the file data 
    $data = chunk_split(base64_encode($data)); 

    // Add file attachment to the message 
    $message .= "--{$mime_boundary}\n" . 
    "Content-Type: {$fileatt_type};\n" . 
    " name=\"{$fileatt_name}\"\n" . 
    //"Content-Disposition: attachment;\n" . 
    //" filename=\"{$fileatt_name}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mime_boundary}--\n"; 
    } 

    mail ("$email", "$subject", "$message", "$headers");