已发送邮件但未在wp_mail()中收到附件

时间:2015-02-24 15:52:45

标签: php wordpress attachment

我是wordpress的新手。我正在尝试发送带附件的邮件。但每次邮件发送但附件不是。我在这里搜索了与此主题相关的几乎所有帖子,但所有解决方案都失败了。我检查了很多次路径,发现它从'uploads'文件夹中是正确的。请帮我。这是我的代码,

<?php

if(isset($_POST['email'])){

$to = $_POST['email'];
$pdf = $_POST['pdf'];

$subject = "Presidency Alumni Annual Report";
$message = "Please download the attachment.";
$headers = 'From: Presidency Alumni Association Calcutta <noreply@presidencyalumni.com>' . "\n";

if($pdf == 'a'){

$attachments = array(WP_CONTENT_DIR . 'uploads/2015/01/Coffee-Mug-Banner.jpg');
}
else if($pdf == 'b'){

$attachments = array(WP_CONTENT_DIR . 'uploads/2014/08/Alumni-Autumn-Annual-2014.pdf');
 }
else{

$attachments = array(WP_CONTENT_DIR . 'uploads/2014/08/Autumn-Annual-2012.pdf');
}

wp_mail($to, $subject, $message, $headers, $attachments);

print '<script type="text/javascript">';
print 'alert("Your Mail has been sent successfully")';
print '</script>';  
}
?>

1 个答案:

答案 0 :(得分:0)

这种情况发生的最可能原因是条件if($pdf == 'a') {...} else if ($pdf == 'b') {...})不正确。检查您的后期HTML表单中是否正确设置了此变量pdf

还要确保常量WP_CONTENT_DIR包含某些内容,即不是空字符串,否则您的附件路径将无效,即最好是访问上传目录,如下所示:

<?php $upload_dir = wp_upload_dir(); ?>

$upload_dir现在包含以下内容(如果成功):

Array (
    [path] => C:\path\to\wordpress\wp-content\uploads\2010\05
    [url] => http://example.com/wp-content/uploads/2010/05
    [subdir] => /2010/05
    [basedir] => C:\path\to\wordpress\wp-content\uploads
    [baseurl] => http://example.com/wp-content/uploads
    [error] =>
)

然后,修改您的代码:

$attachments = array($upload_dir['url'] . '/2014/08/Autumn-Annual-2012.pdf');

请参阅documentation