I got a plugin (kind of sign up form), that offers developers some actions/hooks to add their own stuff. Inside the plugin the function is called like this:
// Allow devs to hook in
do_action( 'after_record_action', $result, $data, $format );
I guess that $data is an array storing the form data. After a visitor uses the signup form I want to send a mail containing $data using wp_mail()
How can I execute the following script using after_record_action? Do I need to add this inside my functions.php?
// get data from $data[] array
$data['email'] = $email;
$data['key'] = $key;
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
I appreciate any help in combining these, as I am not too experienced using php.
Add following code into your currently active theme's functions.php file:
add_action('after_record_action', 'marian_rick_custom_action', 10, 3);
function marian_rick_custom_action ($result, $data, $format){
// get data from $data[] array
$email = $data['email'];
$key = $data['key'];
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
}
if you are interested, how all this really works, check the official docs here
Related
I have created a plugin to send email to user after their donation using paypal. But problem is that user not receiving any email. Please kindly check my code below. I am also using plugin called "PayPal IPN for WordPress" to get information of donation.
<?php
/*
Plugin Name: PayPal IPN Send Email
Plugin URI: http://www.trustedwebsolution.com/
Description: Hook tester for PayPal IPN for WordPress plugin.
Author: Jhishu Singha
Version: 1.0.0
*/
add_action('paypal_ipn_for_wordpress_payment_status_processed', 'paypal_ipn_for_wordpress_payment_email', 10, 1);
function paypal_ipn_for_wordpress_payment_email($posted) {
// Parse data from IPN $posted array
$dir = plugin_dir_url( __FILE__ );
$item_name = isset($posted['item_name']) ? $posted['item_name'] : '';
$payment_gross = isset($posted['payment_gross']) ? $posted['payment_gross'] : '';
$first_name = isset($posted['first_name']) ? $posted['first_name'] : '';
$last_name = isset($posted['last_name']) ? $posted['last_name'] : '';
$payer_email = isset($posted['payer_email']) ? $posted['payer_email'] : '';
$subscr_date = isset($posted['subscr_date']) ? $posted['subscr_date'] : '';
/**
* At this point you can use the data to generate email notifications,
* update your local database, hit 3rd party web services, or anything
* else you might want to automate based on this type of IPN.
*/
if($item_name == 'ATF Donation') {
$to = $payer_email;
$subject = 'Email from Adaptive Training Foundation';
$message = 'Thanks for donation';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $message, $headers );
} else {
// No email
}
}
?>
I am trying to solve it for few days. But no luck. Thanks in advance for help.
I think you should use a different hook. Instead of paypal_ipn_for_wordpress_payment_status_processed use paypal_ipn_for_wordpress_payment_status_completed and see if that helps.
Also, for testing purposes, I would add an email to yourself in the else section of your statement so that you'll get something if the code falls there and you won't think the IPN simply never ran.
i want to send an email with attachment using wordpress' wp_mail function.
With this function it works fine to send emails but the attachment isn't there when i check my email.
function dd_send_email(){
$dd_path = $_POST['upload_image'];
// echo $dd_path;
$email_sent = false;
// get email template data
$email_template_object = dd_get_current_options();
// if email template data was found
if ( !empty( $email_template_object ) ):
// setup wp_mail headers
$wp_mail_headers = array('Content-Type: text/html; charset=UTF-8');
$mail_attachment = $dd_path;
// use up_mail to send email
$email_sent = wp_mail( array( 'example#mail.no') , $email_template_object['dd_emne_forhaandsbestilling'], $email_template_object['dd_email_text_forhaandsbestilling'], $wp_mail_headers, $mail_attachment );
endif;
return $email_sent;
}
The variable $dd_path (something like: http://localhost/norskeanalyser/wp-content/uploads/Testanalyse-2.pdf) contains the path of the file which i do upload from the media uploader in another function.
Thanks for your help!
I did found the answer by myself. Because wp_mail needs the file path like this /uploads/2016/03/example.jpg we have to convert our url to a path like this.
I did achive this by doing the following and thought i can share it with you:
// change url to path
$dd_url = $_POST['upload_image'];
$dd_path = parse_url($dd_url);
// cut the path to right directory
$array = explode("/norskeanalyser/wp-content", $dd_path['path']);
unset($array[0]);
$text = implode("/", $array);
and then i did save the $text into $mail_attachment and called it in wp_mail like above.
I have an email template which i am saving in database. My problem is some part of message are variable means these data are coming from current user data.
For Example My Template is
$message="This is test for $username. I am sending mail to $email."
here $username and $email is coming from current users and it is varying from user to user.
So problem is how to save it in database so i can use it as variable on php page later.
anybody have any idea please help me.your help would be appreciated.
If you really need to store whole template in database, you can save it using your own created constants e.g. [USERNAME], [EMAIL] and then in php script just use str_replace() on them.
$messageTemplate = 'This is test for [USERNAME]. I am sending mail to [EMAIL].';
$message = str_replace(array('[USERNAME]', '[EMAIL]'), array($username, $email), $messageTemplate);
But you can also divide this string and concatenate it with variables from database as follows:
$message = 'This is test for ' . $username . '. I am sending mail to ' . $email . '.';
You can use something like this:
$input = "This is test for {username}. I am sending mail to {email}.";
$tokens = array("username" => $username, "email" => $email);
$tmp = $input;
foreach($tokens as $key => $token)
{
$tmp = str_replace("{".$key."}", $token, $tmp);
}
echo $tmp;
The variables in the string will not be evaluated as variables automatically just because you are adding it to your php scope. You need to eval the string in order for the variables to be replaced:
$username = 'test';
$email = 'test#test.com';
$str = "This is a test for $username. I am sending mail to some person $email.";
echo $str. "\n";
// This is a test for $username. I am sending mail to some person $email.
eval("\$str = \"$str\";");
echo $str. "\n";
// This is a test for test. I am sending mail to some person test#test.com.
For more information, see http://php.net/manual/en/function.eval.php
I've created a static website (clients request) and we have put a email form inside the contacts page. the form works well and sends the data but in the email that is received i get
sam\'s clover\'s (Test Data) how to cleans the data in a static website to remove the \'s and just leave it as 's in the email.
I've tried looking with my keywords not really finding any luck based on the static parts.
any help would be great thanks
This is the vars i'm using at the moment.
$to = STRIP_TAGS($_POST['remail']);
$from = STRIP_TAGS($_POST['semail']);
$phone = STRIP_TAGS($_POST['sphone']);
$subject = STRIP_TAGS($_POST['subject']);
$message = STRIP_TAGS($_POST['message']);
$headers = "From:" . $from;
Use stripslashes():
It unquotes a quoted string. So \' becomes '.
$to = stripslashes($_POST['remail']);
$from = stripslashes($_POST['semail']);
$phone = stripslashes($_POST['sphone']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$headers = "From:" . $from;
You can also use it for arrays:
stripslashes_deep($array_name);
Read about it here: http://php.net/manual/en/function.stripslashes.php
What your after is stripslashes(), but if slashes are being added by PHP automatically from magic quotes then you should check for that, this way your script will run on any server, not just a server with magic quotes on.
Here is a callback function that will loop through all the effected Global vars and fix. You would add this as part of your initialization.
<?php
function process_gpc(&$value, $key){
//magic quotes fix
if (get_magic_quotes_gpc()) {
$key = stripslashes($key);
$value = stripslashes($value);
}
//null byte (string termination) protection
$key = str_replace(chr(0), '', $key);
$value = str_replace(chr(0), '', $value);
}
$inputs = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST, &$_FILES);
array_walk_recursive($inputs, 'process_gpc');
?>
I want my MediaWiki to send a 'Thank You' mail to the author when he creates a new article.
Is any extension available for this method?
Alternatively: I am creating article from a special page. So it is possible to add my own extension and write an email script. But I am confused how to get the email ID of the author.
Use the UserMailer and MailAddress classes:
global $wgPasswordSender, $wgPasswordSenderName;
$from = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
$to = new MailAddress( $user );
$subject = "Thank you!";
$text = "This is a test. Blah blah blah...";
$status = UserMailer::send( $to, $from, $subject, $text );
if ( $status->isGood() ) {
// Great, it worked!
} else {
// Something went wrong, deal with it...
// The $status object will have more information.
}
The MailAddress constructor takes either a User object or an address and a name. $wgPasswordSender is the default e-mail address used by MediaWiki for sending password reset e-mails and other such things; you've hopefully configured it in your LocalSetting.php.