TransWikia.com

wp_mail doen't send mails whit attachment

WordPress Development Asked by StoneSmith on January 25, 2021

On my site I hava two forms witch send email. The one whit no attachment needed is sent correnctly, but the other ony witch has an attachment does not get sent. I am using SMTP config whit 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 storeing and reaching the attachments and simpli useing the wp_mail function to send it like this:

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

On the other form I am useing the same code just whitout the $attachments variable.
The one whitout attachements get sent but the other one does not.
Can anyone help me finx my problem?

2 Answers

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

Answered by rudtek on January 25, 2021

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

Answered by Kapil Yadav on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP