简体   繁体   English

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

[英]Mail sent but attachment not received in wp_mail()

I am new in wordpress. 我是wordpress的新手。 I am trying to send a mail with an attachment. 我正在尝试发送带附件的邮件。 But every time the mail is being sent but the attachment is not. 但每次邮件发送但附件不是。 I searched almost all the post related to this topic here but all the solutions failed for me. 我在这里搜索了与此主题相关的几乎所有帖子,但所有解决方案都失败了。 I checked the path a lot of times and found that it is correct from 'uploads' folder. 我检查了很多次路径,发现它从'uploads'文件夹中是正确的。 Please help me. 请帮我。 This is my code, 这是我的代码,

<?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>';  
}
?>

The most probable reason this to happen is if the condition if($pdf == 'a') {...} else if ($pdf == 'b') {...}) is not true. 这种情况发生的最可能的原因是条件if($pdf == 'a') {...} else if ($pdf == 'b') {...})不是真的。 Check to see if this variable pdf is set properly in your post HTML form. 检查在HTML格式的表单中是否正确设置了此变量pdf

Also make sure that the constant WP_CONTENT_DIR contains something, ie is not empty string, because otherwise your path to the attachments will be invalid, ie it is better to access your uploads directory like this: 还要确保常量WP_CONTENT_DIR包含某些内容,即不是空字符串,否则您的附件路径将无效,即最好访问上传目录,如下所示:

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

The $upload_dir now contains something like the following (if successful): $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] =>
)

Then, modify your code: 然后,修改您的代码:

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

See the documentation . 请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM