简体   繁体   中英

WordPress send mail with uploaded attachment

I have a contact form with a file upload field (CV upload).

I can get the mail and message to send no problems however, the examples I have used from the net to attach the CV from the form arent working.

Is there something I am missing here?

<input type="text" name="fullName" placeholder="Full Name: (required)" required>
<input type="email" name="email" placeholder="Email: (required)" required>
<input type="tel" name="tel" placeholder="Telephone: (required)" required>
<textarea name="message" placeholder="Quick message"></textarea>
<span>Please upload a copy of your cv</span><span><input type="file" name="cv" required></span>


//Handle the file upload and attachment
if ( ! function_exists( 'wp_handle_upload' ) ) {
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['cv'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && ! isset( $movefile['error'] ) ) { 
  $movefile['url'];
}
$attachments = array($movefile['file'] );

$mailoffice = wp_mail('james1@knoppysdev.com', 'New Candidate Application', $messageOffice, $headers, $attachments );

Ok so I got this working by going the long way around.

First I upload the file to the site as an attachment to the page where the form is output.

Then I use the following to get the attachment path, add it as an attachment in wp_mail() and then finally after the mail is send I delete the attachment.

//Handle the CV Upload
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );         

$attachment_id = media_handle_upload( 'cv', $_POST['post_id'] );
$attachments = get_attached_file( $attachment_id );

$mailoffice = wp_mail('james1@knoppysdev.com', 'New Candidate Application', $messageOffice, $headers, array($attachments) );
wp_delete_attachment( $attachment_id, true );

Its rustic but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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