wp_mail doesn't send the attachment in WordPress - wordpress

I have this code and it should work, the email is sent but without the attachment.
$attachments = THEME_DIR . '/resources/img/emails/cropped.png';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Me Myself <me#example.net>';
wp_mail( 'test#example.org', 'subject', 'message', $headers, $attachments );

Okay, turns out that you just need to use ABSPATH.
$attachments = [
ABSPATH . 'wp-content/themes/themename/resources/img/emails/cropped.png',
ABSPATH . 'wp-content/themes/themename/resources/img/emails/facebook.png'
];
// Send the email and the invoice as an attachment.
wp_mail( 'test#email.com', 'New Invoice', 'Message body sent with attachment.', $headers, $attachments );
You can set up also these or just include them in $headers:
add_filter( 'wp_mail_content_type', function ( $content_type ) {
return 'text/html';
} );
add_filter( 'wp_mail_from', 'yoursite_wp_mail_from' );
function yoursite_wp_mail_from( $content_type ) {
return 'contact#yoursite.ca';
}
add_filter( 'wp_mail_from_name', 'yoursite_wp_mail_from_name' );
function yoursite_wp_mail_from_name( $name ) {
return 'Your Site Inc.';
}

Related

Wordpress wp_mail() returns false

I'm new in plugin development. I would like to use this simple function in Wordpress to send mail to users. I saw this code in the documentation everything is simple and straight forward but this code returns false. Why ?
require_once explode( 'wp-content', __FILE__ )[0] . 'wp-load.php';
function send_mail() {
$to = 't.testmail#gmail.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$send_message = wp_mail( $to, $subject, $body, $headers );
if ( $send_message ) {
echo 'Email was sent';
} else {
echo 'Email sending was aborted';
}
}
send_mail();
According to the documentation (https://developer.wordpress.org/reference/functions/wp_mail/) the mail could not be sent.
Reasons could be many. Have you checked the debug log of WordPress (https://wordpress.org/support/article/debugging-in-wordpress/)?

Wordpress/Woo add headers BCC on custom email template

I have gift cards plugin that is sending a custom email template to the user. How do I manipulate that email header in my functions.php file and add a BCC based on a custom order meta? That order meta has an email address in the value.
add_filter( 'woocommerce_email_headers', 'order_completed_email_add_bcc', 9999, 15 );
function order_completed_email_add_bcc( $headers, $email, $order ) {
if ( 'pw_gift_card' == $email ) {
$bcc = get_post_meta( $order->id, 'my_custom_order_meta', true );
$headers .= "Bcc: ".$bcc."" . "\r\n";
}
return $headers;
}
Your code seem to be alright although it needs improvement. You have 3 arguments and not 15. Also for brevity, use $order->get_meta() instead of get_post_meta().
add_filter( 'woocommerce_email_headers', 'order_completed_email_add_bcc', 9999, 3 );
function order_completed_email_add_bcc( $headers, $email_id, $order ) {
if ( 'pw_gift_card' == $email_id && $bcc = $order->get_meta('my_custom_order_meta')) {
$headers .= 'Bcc: ' . $bcc . '\r\n';
}
return $headers;
}

Why does my WordPress email go to spam folder?

I am sending all my WordPress emails using Mailgun SMTP.
Other emails from WordPress arrive to the inbox fine, but I have just created a new function to automatically generate an email when a post is published and it always gets caught by the spam filter.
Can you see why?
Maybe something to do with my headers config?
I have tried sending with and without the attachment, it makes no difference.
add_action( 'publish_post', 'notify_on_publish' );
function notify_on_publish( $post_id ) {
global $post;
$from_name = get_bloginfo( 'name' );
$from_email = get_bloginfo( 'admin_email' );
$to_email = get_post_meta( $post_id, 'contact_email', true );
$attachment = get_post_meta( $post_id, 'post_image', true );
$headers = "From: '$from_name' <$from_email> \r\n Content-type: text/html; charset=" . get_option('blog_charset') . "\r\n";
$subject = "This is a test subject";
$message = "<p>This is some test body text</p>";
$attachment_url = $attachment['guid'];
wp_mail( $to_email, $subject, $message, $headers, $attachment_url );
}

How to display image in message part in wp_mail function?

I am trying to send mail when a post is published. For that I wrote code in my function.php file, the mail is correctly sending, but the featured image is not sent. I want to display the featured image attached to the post. Right now in mail the featured image is not displayed, but the link of fthe eatured image is displayed.
What can I do to achieve my task to display the featured image in the mail? I am attaching the code I have written in function.php file:
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Your Image: ".get_the_post_thumbnail( $post->ID )."
Thanks"
;
wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'mysendmail');
To attach file through wp_mail function, you need to use $attachments parameter in it. In which you need to give absolute file path of attachment.
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$attachments = get_attached_file( get_post_thumbnail_id( $post_id ));
$headers[] = '';
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thanks"
;
wp_mail($author->user_email, $subject, $message, $headers, $attachments);
}
add_action('publish_post', 'mysendmail');
You need to set the content type to text/html for the image to work. You can do that using the wp_mail_content_type filter or by adding a header into your wp_mail() function. Here's an example of the latter.
function mysendmail($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$headers = array('Content-Type: text/html; charset=UTF-8');
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Your Image: ".get_the_post_thumbnail( $post->ID )."
Thanks"
;
wp_mail($author->user_email, $subject, $message, $headers);
}
add_action('publish_post', 'mysendmail');
Try this one, just tested on a local install and it works well :) you need to change the content type for the mail as discussed, also im returning the actual URL of the image (large size).
function set_html_content_type() {
return 'text/html';
}
function mysendmail($post_id) {
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$image = get_the_post_thumbnail_url($post_id, 'large');
$message = '
Hi '.$author->display_name.',<br/><br/>
Your post, '.$post->post_title.' has just been published.<br/>
View post: '.get_permalink( $post_id ).'<br/>
Your Image: <img src="'.$image.'" /><br/><br/>
Thanks';
wp_mail($author->user_email, $subject, $message);
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
add_action('publish_post', 'mysendmail');

wordpress wp_mail is sending my html message as a text attachment

This is the code to send the mail:
$to = $_POST['leadEmail'];
$subject = "my subject";
$message = ( $_POST['leadEmailPref'] == "html" ) ? $messageh : $messagep ;
$headers[] = "From: My Name <ny_name#gmail.com>";
$headers[] = "Content-type: ".$_POST['leadEmailPref'] ;
wp_mail( $to, $subject, $message, $headers );
When I go to my inbox, the message is sent as an attachment and I don't see the message. Obviously I don't want the attachment, and I want the person who receives the email to see the message immediately.
Your Content-type: is wrong. It should be text/html or text/plain, not html.
$headers[] = "Content-type: text/".$_POST['leadEmailPref'] ;
You should try:
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' );
function set_html_content_type() {
return 'text/html';
}
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

Resources