Search code examples
wordpressemailgmailattachmentwp-mail

WP_MAIL() not sending attachments to Gmail


I am trying to send a mail with an attachment using WordPress wp_mail functionality to Gmail([email protected]). Also tried another Gmail. I am not getting email in my mailbox. $runner = wp_mail( $receiver, $subject, $msg, $headers, $attachment );

But, getting mail when I send mail without attachment. $runner = wp_mail( $receiver, $subject, $msg );


But, getting mail to another email ([email protected]) if I send with/without attachment.

Note: [email protected] is a linked with gmail though it's a webmail.

Thanks in advance.

Here is my code.

$attachment = array(WP_CONTENT_DIR . '/uploads/reunion_pdf/reunion_reg_'.$user_id.'.pdf');      

//$headers = 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' . "\r\n\\";

$headers = array();
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';

// $headers[] = 'MIME-Version: 1.0' . "\r\n";
// $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';

$subject = sprintf( __( '[%s] Payment Received', 'wp-user-frontend' ), get_bloginfo( 'name' ) );

$msg = sprintf( __( 'New payment received at %s.', 'wp-user-frontend' ), get_bloginfo( 'name' ) );
$msg .= ' You recieved ৳'.$amount.' for Reunion 2023 from '.$user_name.'('.$user_email.') (User ID : '.$user_id.').';
        
//$adminmail = get_option('admin_email');
//$adminmail = '[email protected]';
$adminmail = '[email protected]';       
$receiver = array($adminmail);
$runner = wp_mail( $receiver, $subject, $msg, $headers, $attachment );

Solution

  • At last, I found the solution. I want to credit Vel.

    Thanks for his valuable comment. I modified his code. And I replace the $headers = array(); $headers[] = 'Content-Type: text/html; charset=UTF-8'; $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>'; with

    $headers[]= "Content-Transfer-Encoding: 7bit". PHP_EOL;

    And working fine.