Unable to send attachment in email using wp_mail() and amazon ses

367 views Asked by At

I have added the following code to send attachments using wp_mail() function. The Runcloud server on which the code is hosted is configured with Amazon SES. On that, the attachment is not sent with the mail, the same code runs perfectly on the local environment with Flywheel.

Code-

add_action(
            'phpmailer_init', function( $phpmailer ) use ( $connection_data ) {
                $phpmailer->IsSMTP();
                $phpmailer->Host = smtp.gmail.com;
                $phpmailer->Port = 587;
                $phpmailer->Username = //username;
                $phpmailer->Password = //password;
                $phpmailer->SMTPAuth = true;
                $phpmailer->SMTPSecure = 'tls';
                $phpmailer->Timeout = 30;
            }
        );

add_filter(
            'wp_mail_content_type', function() {
                return 'text/html';
            }
        );

$file = 'https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg';
$file_name = basename( $file );
$file_name = ! strrpos( $file_name, '.' ) ? $file_name . '.txt' : $file_name;
$content = @file_get_contents( trim( $file ) ) ? wp_remote_get( trim( $file ) ) : $file;
file_put_contents( $file_name, $content['body'] );
$attachments[] = $file_name;

$headers[] = 'From: //Name <//email>';
$send_email = wp_mail( wp_get_current_user()->user_email, 'STMP connection validation', 'STMP connection validation.', $headers, $attachments );
1

There are 1 answers

0
gtamborero On

You have 2 problems, One is to configure correctly the wp_mail function.

I recommend you to use this plugin and configure it to your needs: https://es.wordpress.org/plugins/easy-wp-smtp/

Another problem is to send the attachment. This thread may help you: How to send an email with attachment in wordpress?