我是wordpress的新手。我正在尝试发送一封带有附件的邮件。但每次邮件都在发送,但附件却没有。我在这里搜索了几乎所有与此主题相关的帖子,但所有的解决方案都失败了。我检查了很多次路径,从"上传"文件夹中发现它是正确的。请帮帮我。这是我的代码,
<?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>';
}
?>
发生这种情况的最可能原因是条件if($pdf == 'a') {...} else if ($pdf == 'b') {...})
不成立。检查这个变量pdf
是否在您的postHTML表单中设置正确。
还要确保常量WP_CONTENT_DIR
包含一些内容,即不是空字符串,因为否则你的附件路径将无效,即最好访问你的上传目录,如下所示:
<?php $upload_dir = wp_upload_dir(); ?>
$upload_dir
现在包含如下内容(如果成功):
Array (
[path] => C:pathtowordpresswp-contentuploads2010 5
[url] => http://example.com/wp-content/uploads/2010/05
[subdir] => /2010/05
[basedir] => C:pathtowordpresswp-contentuploads
[baseurl] => http://example.com/wp-content/uploads
[error] =>
)
然后,修改您的代码:
$attachments = array($upload_dir['url'] . '/2014/08/Autumn-Annual-2012.pdf');
请参阅文档。