简体   繁体   English

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

[英]how to set mail with attachment by custom form using wp_mail?

i want send attachment in mail using wp_mail here is my form html 我希望使用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>

here is my code for sending mail 这是我发送邮件的代码

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

but i don't got any mail and when i use this code i got the mail with attachment 但是我没有收到任何邮件,当我使用这个代码时,我收到了带附件的邮件

<?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 . WP_CONTENT_DIR。 '/uploads/2016/10/image026.jpg' is path of my image that already exist in my directory but i want send attachment getting throw from. '/uploads/2016/10/image026.jpg'是我的目录中已经存在的我的图像的路径,但我想发送附件。 please help me. 请帮我。 thanks in advance. 提前致谢。

Attachment should be a full path and the file must be accessible.Instead of making a custom image path every time. 附件应该是完整路径,文件必须是可访问的。而不是每次都制作自定义图像路径。 you should save the file with link in column Like http://example.com/wordpress/wp-content/uploads/request-doc/Grocery_Store_LOGO.jpg and access this path directly in attachment. 你应该保存带有链接的文件,例如http://example.com/wordpress/wp-content/uploads/request-doc/Grocery_Store_LOGO.jpg并直接在附件中访问此路径。 sometime header causes problem.may be you can use like this 某些时候标题导致问题。可能你可以像这样使用

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

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

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