問題描述
好的,我放棄了看過多個例子,包括 this one 。
我收到電子郵件沒有問題,但沒有附件。我是否缺少文件類型的內容/類型?我看過的所有例子只使用 text /html 作為內容類型。
這是我的 (加上斯蒂芬的要求)
if( isset( $_POST['to'] ) && isset( $_POST['from'] ) ) {
global $wpdb;
$to = $_POST['to'];
$from = $_POST['from'];
$name = get_bloginfo('name');
$attachment = $_POST['file'];
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: multipart/mixed; charset=iso-8859-1' . "rn";
$headers .= 'From: ' . $name . ' <' . $from . '>' . "rn";
$subject = 'Send to Kindle';
$msg = 'Yay! Your book has <a href="http://yahoo.com">arrived</a>';
$mail_attachment = array( $attachment );
wp_mail($to, $subject, $msg, $headers, $mail_attachment);
echo 'Email sent';
} else {
echo 'Email not sent';
}
最佳解決方案
wp_mail 的 $attachment 參數獲取文件 (或文件數組),但文件路徑必須完全指定。例如:
<?php
$attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip');
$headers = 'From: My Name <myname@mydomain.com>' . "rn";
wp_mail('test@test.com', 'subject', 'message', $headers, $attachments);
?>
(see Codex) 。看來你的 $_POST['file']可能沒有指定完整的路徑。
附件必須具有文件路徑,而不是 url 。以下為我工作:
$to = $_POST['to'];
$from = $_POST['from'];
$name = get_bloginfo('name');
$headers = 'From: My Name <myname@mydomain.com>' . "rn";
$subject = 'Send to Kindle';
$msg = 'Yay! Your book has <a href="http://yahoo.com">arrived</a>';
$mail_attachment = array(WP_CONTENT_DIR . '/uploads/2012/03/image.png');
wp_mail($to, $subject, $msg, $headers, $mail_attachment);
注意:我也改變了 headers 屬性。我不完全確定你正在嘗試的例子,但這意味着電子郵件的消息在一些電子郵件客户端上不可見。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。