1

In my plugin, there is a contact us form through which users can upload multiple files and it will be uploaded into wp-contents/uploads/2014/10 folder. Uploading is working, but now I need to send email to admin with this uploaded files and entered user data. I don't know how to send mail with attachments.

The code I've tried so far is:

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

This doesn't send any mail also shows error

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'my_custom_email_content_type' not found or invalid function name

Any suggestions to solve this issue?

1 Answer 1

4

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 the wp_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'
    );
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.