WP_mail不发送带有附件的邮件

时间:2016-10-13 作者:StoneSmith

在我的网站上,我有两个发送电子邮件的表格。不需要附件的一个会被正确发送,但另一个只有附件的不会被发送。我正在使用SMTP config whit Postman SMTP插件。

move_uploaded_file($_FILES["cv"]["tmp_name"],WP_CONTENT_DIR .\'/uploads/CV/\'.basename($_FILES[\'cv\'][\'name\']));
move_uploaded_file($_FILES["lm"]["tmp_name"],WP_CONTENT_DIR .\'/uploads/lm/\'.basename($_FILES[\'lm\'][\'name\']));

$attachments = array(
    WP_CONTENT_DIR ."/uploads/CV/".$_FILES["cv"]["name"],
    WP_CONTENT_DIR ."/uploads/lm/".$_FILES["lm"]["name"]
    );
这是我用来存储和访问附件的代码,simpli使用wp\\u mail函数发送它,如下所示:

$sent=wp_mail($s, $subject, $message, $headers,$attachments);
在另一个表单上,我使用相同的代码,只是去掉了$attachments变量。没有附件的一个会被发送,但另一个不会。有人能帮我解决我的问题吗?

2 个回复
SO网友:Kapil Yadav

Try code below.

<?php
$attachments = array();

array_push($attachments, WP_CONTENT_DIR . \'/uploads/my-first-attachment.docx\' );
array_push($attachments, WP_CONTENT_DIR . \'/uploads/my-second-attachment.zip\' );

$to="[email protected]";
$subject="Online: multiple attachment demo through wp_mail of wordpress";
$message="This is testing";
$headers = \'From: NAPSWI online <[email protected]>\';
get_header();
if( wp_mail( $to, $subject, $message, $headers, $attachments) ) {
 // the message was sent...
 echo \'The test message was sent. Check your email inbox.\';
} else {
 // the message was not sent...
 echo \'The message was not sent!\';
}
?>
SO网友:rudtek

我还将尝试使用下面的方式添加到数组中。(参见我的示例)尝试使用这两个文件的实际位置作为测试,然后插入php。我已经列出了3种不同的尝试选项,但您当然应该使用站点上这些文件的实际url/

<?php
$attachments = array(); //may or may not be needed.

$att1 = WP_CONTENT_DIR . \'/uploads/my-1st-attachemnt.zip\';
$att2 = WP_CONTENT_DIR . \'/uploads/my-second-attachment.zip\';


$attachments[] =$att1; //try these as working solution
$attachments[] =$att2; //try these as working solution 


$attachments[] = \'http://www.example.com/wp-content/uploads/my-1st-attachemnt.zip\';  //try this 2nd
$attachments[] = \'/wp-content/uploads/my-second-attachment.zip\';  //try this last


$to="[email protected]";
$subject="Online: multiple attachment demo through wp_mail of wordpress";
$message="This is testing";
$headers = \'From: NAPSWI online <[email protected]>\';
get_header();
if( wp_mail( $to, $subject, $message, $headers, $attachments) ) {
 // the message was sent...
 echo \'The test message was sent. Check your email inbox.\';
} else {
 // the message was not sent...
 echo \'The message was not sent!\';
}
?>

相关推荐

Remove duplicate attachments

情况是这样的:我有一个自动脚本,可以上传附件并将每个附件链接到特定的帖子。由于错误,脚本运行了多次,我有以下内容媒体库中针对单个文件的多个附件帖子(不同的附件帖子具有相同的文件URL)。这些附件中有一个实际上是附在帖子上的。我想做的显然是清理媒体库。我需要在不删除文件的情况下删除附件帖子,并且确保我不删除那些实际附加到他们帖子的帖子。有什么想法吗?