如何使用wp_mail通过自定义表单设置附件邮件?

时间:2016-10-27 06:34:53

标签: php jquery wordpress email email-attachments

我希望使用wp_mail在邮件中发送附件 这是我的表格html

<form method="post"  enctype="multipart/form-data">
<input type="file" name="canopielogo" />
<input type="submit" value="upload" name="canopielogosubmit" id="canopielogosubmit"/>
</form>

这是我发送邮件的代码

<?php
    if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$uploadedfile = $_FILES['canopielogo'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && ! isset( $movefile['error'] ) ) {
echo "File is valid, and was successfully uploaded.\n";
print_r($movefile);
$to = 'mymail@.com';
$subject = 'Custom Canopies Logo';
$message = 'here is logo  '.$movefile['url'];
$imgurl=  str_replace('/home/msulli/public_html/wp-content', '', $movefile['file']);
$headers = 'From: My Name <myname@example.com>' . "\r\n";
$attachments = array(WP_CONTENT_DIR . $imgurl);
$response=wp_mail( $to, $subject, $message,$headers, $attachments );
if($response)
{
    echo 'mail send';
}
 else {
  echo 'mail not send';  
}
} else {
    /**
     * Error generated by _wp_handle_upload()
     * @see _wp_handle_upload() in wp-admin/includes/file.php
     */
   // echo $movefile['error'];
}   
    ?>

但我没有收到任何邮件 当我使用这个代码时,我收到了带附件的邮件

<?php
        if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    $uploadedfile = $_FILES['canopielogo'];
    $upload_overrides = array( 'test_form' => false );
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ( $movefile && ! isset( $movefile['error'] ) ) {
    echo "File is valid, and was successfully uploaded.\n";
    print_r($movefile);
    $to = 'mymail@.com';
    $subject = 'Custom Canopies Logo';
    $message = 'here is logo  '.$movefile['url'];
    $imgurl=  str_replace('/home/msulli/public_html/wp-content', '', $movefile['file']);
    $headers = 'From: My Name <myname@example.com>' . "\r\n";
    $attachments = array(WP_CONTENT_DIR . '/uploads/2016/10/image026.jpg');
    $response=wp_mail( $to, $subject, $message,$headers, $attachments );
    if($response)
    {
        echo 'mail send';
    }
     else {
      echo 'mail not send';  
    }
    } else {
        /**
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
       // echo $movefile['error'];
    }   
        ?>

WP_CONTENT_DIR。 '/uploads/2016/10/image026.jpg'是我的目录中已经存在的我的图像的路径,但我想发送附件。 请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

附件应该是完整路径,文件必须是可访问的。而不是每次都制作自定义图像路径。您应该使用http://example.com/wordpress/wp-content/uploads/request-doc/Grocery_Store_LOGO.jpg列中的链接保存文件,并直接在附件中访问此路径。某些时候标题导致问题。可能你可以像这样使用

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";