在我的插件中,有一个联系我们表单,用户可以通过该表单上载多个文件,并将其上载到wp-contents/uploads/2014/10
文件夹上传正在进行中,但现在我需要向管理员发送电子邮件,其中包含上传的文件和输入的用户数据。我不知道如何发送带有附件的邮件。
到目前为止,我尝试的代码是:
if(isset($_POST[\'Save\'])){
/* store post user data to variables */
if (!function_exists(\'wp_handle_upload\'))
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
$files = $_FILES[\'my_files\'];
$upload_overrides = array(\'test_form\' => false);
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);
$url[] = $movefile[\'url\'];
}
}
$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\' );
if(wp_mail( $to, $subject, $message, $headers, $url ))
echo \'Mail send\';
else
echo \'Failed\';
function my_custom_email_content_type() {
return \'text/html\';
}
这不会发送任何邮件,也会显示错误
警告:call\\u user\\u func\\u array()要求参数1是有效的回调,未找到函数“my\\u custom\\u email\\u content\\u type”,或函数名无效
有什么解决这个问题的建议吗?
最合适的回答,由SO网友:Nicolai Grossherr 整理而成
不知道这是否能解决你所有的问题,但是:
通常,格式良好的代码更容易调试。E、 g.您的代码缺少右大括号。此外,最好用大括号编写if/else(if)语句。好的缩进也有帮助。我建议阅读WordPress - PHP Coding Standards.
关于您的代码:
如wp_mail()
食品法典第页wp_mail_content_type
添加过滤器后应将其移除,以避免出现问题的文件$attachment
参数必须作为路径给定我已经更新了您的代码:
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\'
);
}