wp_mail doen't send mails with attachment

admin2025-01-07  4

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?

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?

Share Improve this question edited Nov 15, 2023 at 15:16 Rup 4,3904 gold badges28 silver badges29 bronze badges asked Oct 13, 2016 at 10:46 StoneSmithStoneSmith 12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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!';
}
?>

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!';
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254223a187.html

最新回复(0)