我想用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>' . "rn";
$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>' . "rn";
$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'是我的图像的路径,已经存在于我的目录中,但我想发送附件得到抛出。请帮帮我。
附件应该是一个完整的路径,并且文件必须是可访问的。而不是每次都自定义图像路径。您应该在类似http://example.com/wordpress/wp-content/uploads/request-doc/Grocery_Store_LOGO.jpg的列中保存带有链接的文件,并在附件中直接访问此路径。有时标头会导致问题。也许你可以这样写
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn";