Don’t know if that will solve all your problems, but:
Generally nicely formatted code is easier to debug. E.g. your code is missing the closing brace. Besides it is better to write if/else(if) statements with braces. Nice indentation helps too. I’d suggest reading the WordPress – PHP Coding Standards.
Regarding your code:
- As noted at the
wp_mail()
codex page thewp_mail_content_type
filter should be removed after adding it to avoid problems. - The file(s) of the
$attachment
parameter have to be given as path.
I’ve updated your code a bit:
if( isset( $_POST[ 'Save' ] ) ) {
function my_custom_email_content_type( $content_type ) {
return 'text/html';
}
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$files = $_FILES[ 'my_files' ];
$upload_overrides = array( 'test_form' => false );
$attachments = array();
foreach ( $files['name'] as $key => $value ) {
if ( $files[ 'name' ][ $key ] ) {
$file = array(
'name' => $files[ 'name' ][ $key ],
'type' => $files[ 'type' ][ $key ],
'tmp_name' => $files[ 'tmp_name' ][ $key ],
'error' => $files[ 'error' ][ $key ],
'size' => $files[ 'size' ][ $key ]
);
$movefile = wp_handle_upload(
$file,
$upload_overrides
);
$attachments[] = $movefile[ 'file' ];
}
}
$to = '[email protected]';
$subject="Contact Us";
$message="Haiii";
$headers[] = 'From: ' . get_option( 'blogname' ) . ' <[email protected]>';
add_filter(
'wp_mail_content_type',
'my_custom_email_content_type'
);
$wp_mail_return = wp_mail(
$to,
$subject,
$message,
$headers,
$attachments
);
if( $wp_mail_return ) {
echo 'Mail send';
} else {
echo 'Failed';
}
remove_filter(
'wp_mail_content_type',
'my_custom_email_content_type'
);
}