• Resolved Pacicio

    (@pacicio)


    I have a custom made plugin that sends an email with some attachments using the function wp_mail. This plugin worked fine until I changed hosting.

    After a lot of tests I found out that the problem is not the new hosting itself but the fact that I disabled the plugin WP Mail SMTP. In the old hosting it was “mandatory” to send the emails but in the new one the email are send even without it so it has been disabled to simplify things and remove a plugin.

    I came to this conclusion after comparing the email sent from the old hosting with the emails sent with the new one, and the only thing that changed was the mailer: PHPMailer 6.8.1 instead of WPMailSMTP/Mailer/smtp 3.11.0

    Am I doing something wrong? Do I need to add some specific headers to be able to send attachments without SMTP? Is this intended or it is a sort of bug?

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • How do you generate the emails with attachments? WordPress itself does not send emails with attachments, which is why I suspect that it is due to an individual implementation that we do not know here.

    Moderator bcworkz

    (@bcworkz)

    wp_mail() will send attachments. The $attachments param must be an indexed array of absolute server paths to the files you want attached.

    Unless you’re coding this yourself, failure is likely due whatever plugin you’re using as threadi implied. It’s possible the plugin’s devs did not consider an attachment feature as necessary. I suggest contacting your plugin’s devs for further guidance.

    Thread Starter Pacicio

    (@pacicio)

    Yes, I’m coding this myself and this is my code: it sends the email after a form is compiled, using AJAX.

    add_action( 'wp_ajax_dm_send_email', 'dm_send_email' );
    function dm_send_email() {
    	
    	$email    = $_POST['email'];
    	$subject  = $_POST['subject'];
    	$headers  = explode( ',', $_POST['headers'] );
    	$body     = $_POST['body'];
    	$attachments_ids = explode( ',', $_POST['attachments'] );
    	
    	foreach ( $attachments_ids as $attachment_id ) {
    		$attachments[] = get_attached_file( $attachment_id );
    	}
    	
    	$headers[] = 'Bcc: ' . wp_get_current_user()->user_email;
    	$content_type = function() { return 'text/html'; };
    	add_filter( 'wp_mail_content_type', $content_type );
    	
    	$email_sent = wp_mail( $email, $subject, stripslashes($body), $headers, $attachments );
    
    	remove_filter( 'wp_mail_content_type', $content_type );
    	echo $email_sent;
    	
    	exit();
    	
    }

    It should be fine because in the old hosting it was working. However any advice is greatly appreciated.

    The code poses some security risks, which is why I would recommend you take a look at this page: https://developer.www.ads-software.com/apis/security/

    According to https://developer.www.ads-software.com/reference/functions/wp_mail/, the attachment parameter should contain a list of paths of the files to be attached. Since this list is based on IDs from an AJAX post parameter, I would recommend that you debug this list. Look at what it contains.

    We can’t give more precise tips with this code alone, because we don’t know what kind of values you receive via AJAX.

    Thread Starter Pacicio

    (@pacicio)

    The IDs send by the form I’m sure that are the IDs the attachment of a post because the form is part of the plugin, but I should add some checks on the PHP code, that’s for sure. Anyway, this is an internal plugin that we do not plan to make public.

    However, on another website with the same hosting configuration everything works fine. I think is probably related to the hosting configuration or the website itself. I’m pretty sure is nothing coding related so I mark this as resolved.

    Thanks for the advice!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_mail do not send attachments with PHPMailer’ is closed to new replies.