auto generate attachment file missing in wp_mail() - wordpress

I am trying to send a mail with an attachment which is generating automatically according to the subscription option. But every time the mail is being sent (in Spam) but the attachment is not. I searched to this topic here but all the solutions are related to the 'uploads' folder.
Here is my code ..
require ( ABSPATH . 'pdfcrowd.php');
try
{
// create an API client instance
$client = new Pdfcrowd("apiname", "apikay");
// convert a web page and store the generated PDF into a $pdf variable
$pdf = $client->convertFile( ABSPATH . 'invoice_html.php');
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"invoice.pdf\"");
//$to = $invoice_email;
$to = "moyen#gmail.com";
$subject = "Invoice for your online package.";
$message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin#gmail.com');
$attachments = $pdf;
// send the generated PDF
//echo $attachments;
$wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
any help?
N.B:Since I managed to output the pdf file in the browser to save but I would like to save this pdf file in a directory and then will send as attachment. What will be the best with this code above ?
Thanks

Finally I am able to send the pdf in email by the following modification code below....
1. First I just save the converted pdf in the uploads folder then
2. I take the pdf for the attachments
require ( ABSPATH . 'pdfcrowd.php');
try
{
// create an API client instance
$client = new Pdfcrowd("apiname", "apikay");
// converted php file and store the generated PDF inside uploads
$fd = fopen( ABSPATH . 'wp-content/uploads/invoice.pdf', 'wb');
$client->convertFile( ABSPATH . 'invoice_html.php', $fd );
fclose($fd);
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
//header("Content-Disposition: attachment; filename=\"invoice.pdf\"");
//$to = $invoice_email;
$to = "moyen#gmail.com";
$subject = "Invoice for your online package.";
$message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin#gmail.com');
// take the file from the uploads folder
$attachments = array( ABSPATH . '/wp-content/uploads/invoice.pdf' );
// send the generated PDF
//echo $attachments;
$wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}

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/)?

Sending email notification to users when their post is published in WordPress. Why Multiple times?

I have a WordPress site where users can post from the front-end and the post status goes as Draft.
Now when I publish the post from Admin panel, the notification email is sent more than one time. I need to send email once.
Below my code:
if (is_admin()) {
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post publish notification";
$headers = 'From: '.get_bloginfo( 'name' ).' <my_email#gmail.com>' . "\r\n";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thank You, Admin"
;
wp_mail($author->user_email, $subject, $message, $headers);
}
add_action('publish_post', 'notifyauthor');
}
I tried current_user_can('administrator') insteed to is_admin(), but same result I got.
Many hooks will actually run more than one time. The simple solution is to add a counter by way of post_meta after the first iteration, then check it doesn't exist. This isn't tested, but should work.
function notifyauthor($post_id) {
if (is_admin() && !(metadata_exists('post', $post_id, 'sent_notification_email'))) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post publish notification";
$headers = 'From: '.get_bloginfo( 'name' ).' <my_email#gmail.com>' . "\r\n";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thank You, Admin";
wp_mail($author->user_email, $subject, $message, $headers);
// Set a meta key as a counter
update_post_meta($post_id, 'sent_notification_email', '1');
}
}
add_action('publish_post', 'notifyauthor');

Wordpress wp_mail function sends email twice

I am using a cron event and adding a PHP code to send emails to users of my wordpress site. But due to some reason my wp_mail function is sending emails twice to all the users.
Here is my code, can anyone please tell me whats wrong with my code ?
$args = array(
'role' => 'employee',
'order' => 'ASC'
);
$all_users = get_users($args);
foreach ($all_users as $user) {
$to = esc_html($user->user_email) ;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: TEST <test#test.com>' . "\r\n";
$subject = "Regarding auto emails";
$message = "<p>Dear Employee/Consultant,</p>
<p >Please send in your submissions</p>";
wp_mail($to, $subject, $message, $headers);
}
You can write mail function in this way
if(wp_mail($to, $subject, $message, $headers)){ // Condition check if mail has been sent
continue; // Continue to next iteration
}
Your PHP looks okay to me.
I have had luck scheduling cron in WordPress using the approach shared here (duplicated below). And by structuring the mail function within loops as follows:
<?php
$to = 'email#website.com';
$subj = 'Subject';
$body ='
<br/>
Dear Recipient,<br/>
<br/>
Here is an email.<br/>
<br/>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Website.com <email#website.com>' . "\r\n";
wp_mail( $to, $subj, $body, $headers );
?>
You can install a plugin like Cron Manager to verify that the cron event is scheduled. "custom_cron_event" (or whatever name you choose for your function) should be listed as a scheduled cron if you've activated the plugin and it is working correctly.
Cron schedule via plugin:
<?php
/*
Plugin Name: Custom Plugin
Plugin URI:
Description: Adds function on cron
Author:
Version: 1.0
*/
/*
* When this plugin is activated, schedule
*/
function activate_custom_cron(){
wp_schedule_event( time(), 'daily', 'do_custom_cron_event');
}
register_activation_hook(__FILE__, 'activate_custom_cron');
/*
* When this plugin is deactivated, clear_schedule cron
*/
function deactivate_custom_cron(){
wp_clear_scheduled_hook('do_custom_cron_event');
}
register_deactivation_hook(__FILE__, 'deactivate_custom_cron');
/*
* Function
*/
function custom_cron_event() {
/*
* This is where your function goes
*/
}
add_action( 'do_custom_cron_event', 'custom_cron_event' );
?>

error in sending attachment mail in wp_mail using wordpress

I am using the following code :
add_filter( 'wp_mail_content_type', 'set_html_content_type');
function set_html_content_type() {
return 'text/html';
}
$headers = 'From: xxxn <info#xxx.co.in>' . "\r\n";
$subject = 'Your Axxxxrship 2017';
$msg = 'Dear '.ucfirst($get_s[0]->name)."<br/><br/>
Thank You for taking intrest in Scholarship 2017. We have recieved Payment from you. Please find the admit card for your Future
reference. <br/><br/>
Thanks & Regards<br/>
xxxxxx
";
$up = __DIR__ . '/menu-pages/admit_card/'.$stid.'.pdf';
$mail_attachment = array($up);
wp_mail($to, $subject, $msg, $headers,$mail_attachment);
remove_filter( 'wp_mail_content_type', 'set_html_content_type');
But I am trying to send the mail. The mail is sent successfully but I didnt recieve any attachment in the mail.
How I can trace that what is the problem. Path of sending file is :
/home/xcdx/public_html/wp-content/plugins/schloarship/menu-pages/admit_card/20108.pdf

How to send automated emails to each user about their content in Drupal

I have a Drupal site which lets users create an account and publish their own content. However I have been trying for hours with no luck to try and set up an email service which sends an email to all the users with content every week for example. The email will show them how many page hits their content has got. Any help will be much appreciated! Cheers
I think in this case you have to use drupal rules modeule.
Me and my colleague have found a solution. We first created a rule which will run on a cron job to execute the custom php below.
$type = '<node_type>';
$articles = db_select('node')
->fields('node', array('nid', 'title'))
->condition('type', $type, '=')
->execute()
->fetchAllKeyed();
$nodes = array();
foreach ($articles as $nid => $title) {
$nodes[] = node_load($nid);
}
$checked = array();
foreach($nodes as $node) {
if(!in_array($node->nid, $checked)) {
$statistics = statistics_get($node->nid);
$email = $node->field_email['und'][0]['email'];
$subject = 'Content view update';
$headers = 'From: you#domain.com' . "\r\n" .
'Reply-To: you#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = 'Hi,
Your content has been viewed ' . $statistics . ' times';
global $language;
$params['subject'] = t($subject);
$params['body'] = array(t($message));
drupal_mail('smtp', 'smtp-test', $email, $language, $params);
$checked[] = $node->nid;
}
}
The php script loops through all of the custom content nodes with type $type and retrieves the title, email and statistics then emails the information to the content owner.
If anyone has a better solution please can you comment.

Resources