0

On my site I have two forms witch send email. The one with no attachment needed is sent correctly, but the other one which has an attachment does not get sent. I am using SMTP config with Postman SMTP plugin.

move_uploaded_file($_FILES["cv"]["tmp_name"],WP_CONTENT_DIR .'/uploads/CV/'.basename($_FILES['cv']['name']));
move_uploaded_file($_FILES["lm"]["tmp_name"],WP_CONTENT_DIR .'/uploads/lm/'.basename($_FILES['lm']['name']));
    
$attachments = array(
    WP_CONTENT_DIR ."/uploads/CV/".$_FILES["cv"]["name"],
    WP_CONTENT_DIR ."/uploads/lm/".$_FILES["lm"]["name"]
    );

This is the code I use for storing and reaching the attachments and simply using the wp_mail function to send it like this:

$sent=wp_mail($s, $subject, $message, $headers,$attachments);

On the other form I am using the same code just without the $attachments variable.
The one without attachments get sent but the other one does not. Can anyone help me find my problem?

2 Answers 2

0

Try code below.

<?php
$attachments = array();

array_push($attachments, WP_CONTENT_DIR . '/uploads/my-first-attachment.docx' );
array_push($attachments, WP_CONTENT_DIR . '/uploads/my-second-attachment.zip' );

$to="[email protected]";
$subject="Online: multiple attachment demo through wp_mail of wordpress";
$message="This is testing";
$headers = 'From: NAPSWI online <[email protected]>';
get_header();
if( wp_mail( $to, $subject, $message, $headers, $attachments) ) {
 // the message was sent...
 echo 'The test message was sent. Check your email inbox.';
} else {
 // the message was not sent...
 echo 'The message was not sent!';
}
?>
1
  • Unfortunately does not work :( the mail doesn't get sent
    – StoneSmith
    Oct 13, 2016 at 10:56
0

I would also try adding to the array using the fashion below. (see my example) try using the actual location of the 2 files as just a test, then insert the PHP. I've outlined 3 different options to try out, but of course you should use the actual URL on your site to those files/

<?php
$attachments = array(); //may or may not be needed.

$att1 = WP_CONTENT_DIR . '/uploads/my-1st-attachemnt.zip';
$att2 = WP_CONTENT_DIR . '/uploads/my-second-attachment.zip';

$attachments[] = $att1; //try these as working solution
$attachments[] = $att2; //try these as working solution 
$attachments[] = 'http://www.example.com/wp-content/uploads/my-1st-attachemnt.zip';  //try this 2nd
$attachments[] = '/wp-content/uploads/my-second-attachment.zip';  //try this last


$to = "[email protected]";
$subject = "Online: multiple attachment demo through wp_mail of wordpress";
$message = "This is testing";
$headers = 'From: NAPSWI online <[email protected]>';
get_header();

if ( wp_mail( $to, $subject, $message, $headers, $attachments) ) {
   // the message was sent...
   echo 'The test message was sent. Check your email inbox.';
} else {
   // the message was not sent...
   echo 'The message was not sent!';
}

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.