Contact Form 7 wpcf7_before_send_mail hook - php

Using Contact Form 7, I need to add additional content to the 'body' of the message. Based on the googles and many searches here, I came up with this:
add_action ('wpcf7_before_send_mail', 'cellarweb_add_to_message', 10, 2);
function cellarweb_add_to_message($additional_mail, $contact_form) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$extracontent = "<p>This is more text for the message body.</p>";
$mail = $wpcf7->prop('mail');
$mail['mail']['body'].= $extracontent ;
// Save the email body
$wpcf7->set_properties(array(
"mail" => $mail
));
return $wpcf7;
}
I suspect I am not using the right object, or not modifying the right object.
What is the proper way to add text to the CF7 message body?

I think you are missing space while appending the content. Please check the below code.
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
function wpcf7_add_text_to_mail_body($contact_form){
$mail = $contact_form->prop( 'mail' );
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
}
$extracontent = "<p>This is more text for the message body.</p>";
$mail['body'] .= '<br>';
$mail['body'] .= $extracontent
$contact_form->set_properties( array( 'mail' => $mail ) );
}

Related

Send an email notification to customer when a specific coupon code is applied in WooCommerce

I am trying to send an email to the customer after he has used a specific promo code 'FREECLASS' while checking out.
What I do is send the customer a code 'FREECLASS' after signing up. I want the customer to get an additional custom message after he uses that code.
Based on Send an email notification when a specific coupon code is applied in WooCommerce answer code, this is what I have done up until now but it is not working.
add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
if( $coupon_code == 'FREECLASS' ){
// Get user billing email
global $user_login;
$user = get_user_by('login', $user_login );
$email = $user->billing_email;
$to = "$email"; // Recipient
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied'), $coupon_code );
wp_mail( $to, $subject, $content );
}
}
This is my first WooCommerce project so some help would be really appreciated.
There is no need to use global variables, via $user_id you can get the WC_Customer instance Object and then the billing email address
wp_mail() - Sends an email, similar to PHP’s mail function
So you get:
function action_woocommerce_applied_coupon( $coupon_code ) {
// NOT logged in, return
if ( ! is_user_logged_in() ) return;
// Compare
if ( $coupon_code == 'freeclass' ) {
// Get user ID
$user_id = get_current_user_id();
// Get the WC_Customer instance Object
$customer = New WC_Customer( $user_id );
// Billing email
$email = $customer->get_billing_email();
// NOT empty
if ( ! empty ( $email ) ) {
// Recipient
$to = $email;
$subject = sprintf( __('Coupon "%s" has been applied', 'woocommerce' ), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer', 'woocommerce' ), $coupon_code );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $to, $subject, $content, $headers );
}
}
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );

Send custom WooCommerce mail with user credentials after order with certain product

I am trying to send a custom WooCommerce mail with user credentials after an order with certain product. To explain further: Someone buys a product
I have to check if the order contains a certain product id
I have to check if the customer is registered as a user already, if not it should create a user
Then it should send a custom mail with those credentials to the customer
I am struggling to get the following code to work. Especially I dont know how I can add my custom email-message with the credentials and a custom login link. Do you know a solution?
add_action( 'woocommerce_thankyou', 'check_order_product_id' );
function check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_email = $order->billing_email;
//create custom mail
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order_email;
$subject = __("Deine Login-Daten für Geomap", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
function email_exists( $order_email ) {
$user = get_user_by( 'email', $order_email );
if ( $user ) {
return $wp_new_user_notification_email;
}
else {
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $random_password, $order_email );
return $wp_new_user_notification_email;
}
}
}
}
}
Because you mentioned low barrier to entry being very important...
I'd recommend linking to your thank you page and having the registration link there too. You can use SSO (Google/Amazon/etc.) options so that users don't need to register directly through WordPress.
I'm assuming the product ID has some kind of activation code? You can send that, encrypted, as a parameter to the registration page so that it's registered in their session. Once logged in, you have attached their activation code to the user account, and you're done.
Steps:
Customer buys
If they have an account: send them the link, and they're done.
If they don't:
a. Send them an email with a link with the encrypted activation code.
b. They will have to register (through SSO or through WordPress directly).
c. Once registered, you attach their product to their user account, and redirect them to the page they care about.
Hey for the question "how I can add my custom email-message with the credentials and a custom login link",
In your code, there is this line
$template = 'emails/my-custom-email-i-want-to-send.php';
you could add the credentials there, you may pass other arguments as the second parameter of this functionwc_get_template_html
second, the code order will not work for what you are trying to achieve
in this line, you are sending the email
$mailer->send( $recipient, $subject, $content, $headers );
And after that, there is a foreach loop for all the items in the order and creates a function looking if the user exists.
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
function email_exists( $order_email ) {
This function is not been called.
I will suggest to take out that function from the foreach loop and call it inside if your goal is to check only if the product XYZ exists
and all of it before the email
function check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_email = $order->billing_email;
//create custom mail
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
function send_custom_email($order_email, $order){
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order_email;
$subject = __("Deine Login-Daten für Geomap", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
}
function email_exists( $order_email ) {
$user = get_user_by( 'email', $order_email );
if ( $user ) {
send_custom_email($order_email, $order);
//not sure where this variable was comming from
return $wp_new_user_notification_email;
}
else {
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $random_password, $order_email );
//not sure where this variable was comming from
return $wp_new_user_notification_email;
}
}
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
email_exists( $order_email );
}
}
}
Now as a conclusion, WP_user class doesn't give access to the password for security reasons, and they don't send it by email, you should not either is a very bad practice.
I will suggest you create the account and let WP send the default email and then you send another email with the link to your custom page, but not with the credentials.
That way they will receive two emails one secure and one with the special conditions.

Joomla: onContentAfterSave not triggered for articles

I'm developing a Joomla plugin with onContentAfterSave event to send an email after saving a new articles.
the event are triggered when i save a new menu item or a new category.
but not for new article.
Joomla! 3.7.5
public function onContentAfterSave($context, $article, $isNew){
$link = JRoute::_(ContentHelperRoute::getArticleRoute( $article->id, $article->catid ));
$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
$sender = array(
$config->get( 'mailfrom' ),
$config->get( 'fromname' )
);
$mailer->setSender($sender);
$user = JFactory::getUser();
$recipient = $user->email;
$recipient = array($recipient);
$mailer->addRecipient($recipient);
$body = '<p>Bonjour</p>'
.'Un nouveau article a été ajouté.';
$mailer->isHtml(true);
$mailer->Encoding = 'base64';
$mailer->setSubject('Nouveau article - BBN Times');
$mailer->setBody($body);
$send = $mailer->Send();
if ( $send !== true ) {
echo 'Error sending email: ';
} else {
echo 'Mail sent';
}
return true;
}
The onContentAfterSave is triggered by the Joomla core, and not by the extension's model, which means it should be triggered by any content saving.
I can think of 2 reasons why it is not triggered in your case:
You have a condition in your constructor checking for the extension type and only allowing certain extensions to use that event (or somewhere else in your plugin).
You have an error in the code above.

PHP returning extra whitespace with return statement?

My code works, but for some strange reason when I return a string it adds extra space and I think a new line as well.
The code below works fine. However the length of this when fetched with Ajax is 11, the string success has a length of seven so this isn't right and it also doesn't equal to 'success' when comparing.
I've looked through a few other questions but all the solutions they offer don't seem to be working for me. No trailing spaces anywhere as far as I can see. And it's also not coming from any of the files I'm requiring.
<?php
/*
Plugin Name: Quotation Rest API
Plugin URI:
Description: A rest API for sending quotation request through to email using Sendgrid.
Version: 1.0
Author: Niels
Author URI:
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'offerte', '/send',array(
'methods' => 'POST',
'callback' => 'send_mail',
) );
} );
include('MailChimp.php');
use \DrewM\MailChimp\MailChimp;
function send_mail( WP_REST_Request $request) {
$data = $request['data'];
$name = htmlspecialchars($request['name']);
$email = htmlspecialchars($request['email']);
$comment = htmlspecialchars($request['comment']);
$website = htmlspecialchars($request['url']);
$company = htmlspecialchars($request['company']);
$tel = htmlspecialchars($request['tel']);
$mailchimp = htmlspecialchars($request['mailchimp']);
$dataArray = json_decode($data, true);
ob_start();
include('mail.php');
$mailHTML = ob_get_clean();
$success = 'success';
if (!empty($mailchimp)) {
// add email to mailchimp list
$client = new MailChimp('-us14');// get from setttings
$list_id = ' ';// get from setttings
$result = $client->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
}
$to = " #icloud.com";// get from setttings
$subject = "Offerte van " . $name;// // get from setttings (maybe...)
$message = $mailHTML;
if(wp_mail( $to, $subject, $message)){
return $success;
} else {
// return 'error';
}
}
?>

How can I add a "Send copy of email to self" checkbox in Contact Form 7?

I'm trying to use the following, but I cannot get this code to work.
function check_mail_send_contactform($cf7) {
//get CF7's mail and posted_data objects
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$mail = $cf7->prop( 'mail' );
if($posted_data['contact_sendmail'][0]) { //if Checkbox checked
$mail2 = $cf7->prop( 'mail_2' ); //get CF7's mail_2 object
//now set sender's address to mail2's recipient
$mail2['recipient'] = $mail['sender'];
$mail2['active'] = true;
$cf7->set_properties( array( 'mail_2' => $mail2 ) );
}
return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');
I've dumped mail_2 and it seems to be setup correctly; active, recipient set and email message setup w headers, etc.

Categories