Php Can';从计算机附加文件时,不要使用wp_mail接收任何文件
我收到的电子邮件附件,但它只是一个没有扩展名的文件。(附件文件为“文件”,无扩展名)。 这是我的密码Php Can';从计算机附加文件时,不要使用wp_mail接收任何文件,php,wordpress,email,file-upload,Php,Wordpress,Email,File Upload,我收到的电子邮件附件,但它只是一个没有扩展名的文件。(附件文件为“文件”,无扩展名)。 这是我的密码 $to = $user->user_email; $subject = $_POST['subject']; $body = $_POST['description']; $file = $_FILES["attachment_news"]["tmp_name"]; $content = parse_url($file); $files =
$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 );
这是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>
您必须传递附件的多部分/混合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 );
您还应该添加文件名
$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 );
谢谢大家参与帮助我。我不得不使用mail
而不是wp\u 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");
您正在附加图像吗?是的,我正在附加图像。我的意思是$files
应该在数组中,将$files
替换为数组($files)
当您打印$files
时,您会得到什么?打印$files
给出文件的临时路径<代码>/tmp/php6xXL7S