WP_mail文件附件没有放在上载文件夹中?

时间:2014-07-02 作者:user13286

我有一个表单,希望用户能够选择要上载的图像。图像临时上传到wp content/uploads文件夹,附加到电子邮件,然后销毁。

现在,表单正在提交,我可以看到图像正在响应标题中处理,但电子邮件中没有图像,上载文件夹中也没有图像文件。是否有人发现此代码有任何问题?

以下是表格:

<form id="shareForm" action="<?php echo get_template_directory_uri(); ?>/library/file-form.php" method="post" enctype="multipart/form-data">
    <input type="text" id="name" name="name" placeholder="Name" />
    <textarea rows="4" cols="50" name="message" id="message" placeholder="Message"></textarea>
    <input type="file" id="photo" name="photo" class="upload">
    <input type="submit" name="submit" value="Submit">
</form>
下面是我的PHP邮件脚本:

<?php
    require( \'../../../../wp-load.php\' );

if(isset($_POST["name"])){

    $name = $_POST["name"];
    $message = $_POST["message"];

    $fName = str_replace(array(\' \', \',\', \'\\\'\' ), \'-\');

    $date = new DateTime(null, new DateTimeZone(\'America/Chicago\'));
    $myDate = $date->format("m/d/Y H:i");
    $fileDate = $date->format("m-d-Y");
    $homeUrl = home_url(\'/\');

    if($_FILES["file"]["type"] != ""){

        $allowedExts = array("jpg", "png", "gif");
        $extension = end(explode(".", $_FILES["file"]["name"]));
        $newFileName = $fName . $fileDate."." .$extension;

        if ($_FILES["file"]["error"] > 0) {
            $myPath =  $homeUrl . \'share/?upload=error\';
            wp_redirect($myPath); exit;     

        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);
        }

    }

    $to = \'[email protected]\';
    $subject = $fName . " submitted a photo";

    $message = "Date: " . $myDate . " EST \\n";
    $message .= "Message:  " .$message. "\\n";

    $attachments = array(WP_CONTENT_DIR . "/uploads/user-submitted/".$newFileName); 
    $headers[] = \'From: Photo Submissions <[email protected]>\';

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

    unlink(WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);

    $myPath =  $homeUrl . \'share/?upload=success\';
    wp_redirect($myPath); 
    exit;   

}

?>

1 个回复
最合适的回答,由SO网友:TBI Infotech 整理而成

附件应始终使用绝对文件系统路径。

还要更改Content-Type 您应该使用wp_mail_content_type 滤器

<?php
function my_custom_email() {
    $to  = \'[email protected]\';
    $subject = \'WordPress wp_mail\';
    $message = \'
    <html>
    <body>
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>
        </table>          
    </body>
    </html>
    \';

    $attachments = array(  WP_PLUGIN_DIR . \'/my-plugin/uploads/sample_photo_01.jpg\' );
    $headers[] = \'From: \'.get_option( \'blogname\' ).\' <\'.get_option( \'admin_email\' ).\'>\';
    add_filter( \'wp_mail_content_type\', \'my_custom_email_content_type\' );
    return wp_mail( $to, $subject, $message, $headers, $attachments );
}

function my_custom_email_content_type() {
    return \'text/html\';
}
我已将整个代码放置在一个函数中,以便wp\\u mail\\u content\\u type筛选器仅适用于此电子邮件。

资料来源:

http://codex.wordpress.org/Function_Reference/wp_mail

http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type

或者您可以尝试此代码

 $attachments = array( WP_CONTENT_DIR . \'/uploads/file_to_attach.zip\' );
   $headers = \'From: My Name <[email protected]>\' . "\\r\\n";
   wp_mail(\'[email protected]\', \'subject\', \'message\', $headers, $attachments );

结束