CF7 need to send two different mail - wordpress

I've created a form with contact form 7, and I need to send two e-mails, one with all the data that I pick up from the form, another one with a "thank you" message and other important information, these informations are statics, all the same for every user that clicks in the submit button.
So my problem is this email, CF7 can send only one type of mail to multiple users, but not two differents mail to two differents users. The second mail needs to use the [your-mail] (the mail that the users write in the form).
I've discovered the on_sent_ok function that allows me to open a page or something else after the submit, but I have no idea how to send this different mail.

It's been a while from your request, but I write here my solution for future needs.
You can reach the goal by following 2 steps:
A) handle the sent mail Contact Form 7 event.
function your_wpcf7_mail_sent_function( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ($submission && "New hospital" == $title) {
$posted_data = $submission->get_posted_data();
//$posted_data is an associative array that contains all the form input values,
//so you can use it to proceed with B step
}
}
add_action( 'wpcf7_mail_sent', 'wpcf7_mail_sent_event_triggered' );
If you're not sure on where place this code my suggestion is to add it at the beginning of your theme's function.php file.
B) send a custom mail using wp_mail API.
If you take a look at the docs you'll find that the API is quite easy to understand.
$to = 'sendto#example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Obviously you can customize the body message using or not the $posted_data values according to your needs.

Related

A Webhook contact form for Discord in wordpress

So I'll start out by saying, that I am a bit new to this.
So I have this website I'm currently making. It's a guild website for World of Warcraft, and we want to be able to have new people being able to apply for membership.
Making the contact form is easy enough through plugins, but this is in theory what I wish to make:
A contact form where when filled in, the application form will push a notification to a webhook set in Discord where when new applicants happen, a message in a channel will be made, notifying the leaders about it.
Do I need to create a plugin myself, or is there any plugin that can offer this functionality?
I had the same needs, after sometime i found a way to do it.
Its actually very simple with WPForms.
WPForms has hooks so you can easily track forms submitions with the wpforms_process_complete hook. This hook allows you to track ALL WPForms sumbission. But maybe you'd like to have different forms. If you wish to track only a specific form, you can add the form id to the end of the hook name.
In my case i had many different forms which are being handled in various different ways, so i had to split them. When a form is being created in WPForms, it receives an ID so does the fields of the named form.
In my case after my form was created it had the following id :
The hook function.
As explained on the Discord Webhook page, Webhooks are a low-effort way to post messages to channels in Discord. They do not require a bot user or authentication to use. The endpoint supports both JSON and form data bodies. In my case i went for JSON.
As explained here you just need to use one of the the content file or embeds field. On this example i will just send a message, so i'll be using the content field.
Once the above instructions applied, you should end up with something close to the following function :
if ( ! function_exists( 'discord_form_submission' ) ) :
/**
* This will fire at the very end of a (successful) form entry.
*
* #link https://wpforms.com/developers/wpforms_process_complete/
*
* #param array $fields Sanitized entry field values/properties.
* #param array $entry Original $_POST global.
* #param array $form_data Form data and settings.
* #param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite.
*/
function discord_form_submission( $fields, $entry, $form_data, $entry_id )
{
// You have to replace this url by your discord webhook.
$endpoint = 'https://discord.com/api/webhooks/{webhook.id}/{webhook.token}';
// This is the content you can put anything you wish.
// In my case i needed the Name, Class, and the Level of the players.
$content = "**Name :** " . $fields[1]['value'] . PHP_EOL;
$content .= "**Class :** " . $fields[2]['value'] . PHP_EOL;
$content .= "**Level :** " . $fields[3]['value'] . PHP_EOL;
// WP has its own tool to send remote POST request, better use it.
wp_remote_post( $endpoint , [
'headers' => [
'Content-Type' => 'application/json; charset=utf-8'
],
'body' => wp_json_encode([ // Same for the JSON encode.
'content' => $content,
]),
'method' => 'POST',
'data_format' => 'body'
]);
}
endif;
This function must be added in the functions.php file of your theme.
Last but not least, with the help of the WP add_action Function you need to hook up with the wpforms_process_complete hook. In my case since i want to only hook up to the form with the id 1862 i have added the id at the end of the hook which gives us the following code :
add_action( 'wpforms_process_complete_1862', 'discord_form_submission', 10, 4 );
This code must be added in the functions.php file of your theme after our newly added function.

create event custom and send email to user automatically at a specified time

I intend to send an email to a user at a specified time.
I use wp_schedule_single_event to send email to user with this code:
function send_email() {
$to = "ex#example.com";
$subject = "test function wp_mail";
$message = "this is a body text massage....";
wp_mail($to, $subject, $message);
}
add_action( 'event','send_email' );
wp_schedule_single_event( time() + 180, 'event' );
But this email code is sent every three minutes and I do not want to be that way
And I just want to send an email once.
please help me!
You need to add your code inside a function or a response otherwise it will
schedule this event on every site visit :
function send_email() {
// send mail
}
add_action( 'event','send_email' );
// put this line inside a function,
// presumably in response to something the user does
// otherwise it will schedule a new event on every page visit
wp_schedule_single_event( time() + 180, 'event' );

Disable auto login upon registration in Wordpress

[update1] I am using the ClassiCraft theme and I have no idea where to customize the login and register forms
[update2] I know that the registration process does not go through wp_authenticate because I redefined it inside a plugin of mine
I am quite new in the Wordpress world (actually just got my hands on it for the first time yesterday) and I am having some difficulties finishing up a little project I am working on.
The project is rather simple (or so I thought) and consists in adding a confirmation link to email received upon registration in order to validate the email address provided to prevent using fake emails that the registrar does not even own.
I am about all done except that once I hit the register button it leads to log in the freshly created user.
I googled stuff like "wp disable auto login on registration" and whatnot but I have not been able to find anything that worked. I even tested a few plugins supposed to be doing exactly what I need but none of them worked.
Also, I am not using any plugins for the registration/login forms and it appears that the code in the wp-login.php file is actually not even used...
Would anyone have an idea? Thanks
Okay, so without an access to the theme, i can't really answer you.
But i can tell you what I would try.
1. Add action on user_register hook, to add a post meta that will be useful to check if user has confirm his email.
add_action( 'user_register', 'add_has_confirm_email_user_meta');
function add_has_confirm_email_user_meta( $user_id ) {
update_user_meta( $user_id, 'has_confirm_email', 0 );
}
2. Prevent the user from log in automatically after registration.
Here i can't tell you the hook that will works for you. For example, the hook for the wordpress registration is user_register, but if you have woocommerce, the hook I will use, would be woocommerce_registration_redirect. So try to find what hook is available after the registration with your theme.
In all case, the code in the function would be something like :
function custom_registration_redirect() {
// Log out the user
wp_logout();
// The login url could be an other, with woocommerce for example it is : get_permalink(get_option('woocommerce_myaccount_page_id')
$login_url = wp_login_url();
// Redirect on it
wp_redirect( $login_url);
exit;
}
It will also be necessary, to add a message on this page to alert the user, that he will receive an email to confirm his account.
3. Prevent user from login when he submit the log in form
Add action on wp_login hook to achieve that.
add_action('wp_login', 'prevent_user_from_login', 10, 2);
function prevent_user_from_login($user_login, $user = null ) {
if ( !$user ) {
$user = get_user_by('login', $user_login);
}
if ( !$user ) {
// not logged in
return;
}
// Get user meta
$has_confirm_email = get_user_meta( $user->ID, 'has_confirm_email', true );
if ( $has_confirm_email == '0' ) {
// Clear cookies, a.k.a log user out
wp_clear_auth_cookie();
$login_url = wp_login_url();
$login_url = add_query_arg( 'has_confirm_email', '0', $login_url);
wp_redirect( $login_url );
exit;
}
}
4. Add message on log in page if we get the has_confirm_email to 0
add_filter('login_message', 'has_not_confirm_email_login_message');
function has_not_confirm_email_login_message($message) {
if ( isset( $_GET['has_confirm_email'] ) && $_GET['has_confirm_email'] == 0 ) {
$message = '<div id="login_error">You have not confirmed your email.</div>';
}
return $message;
}
5. Send the email with a link to confirm his email.
You will need to generate a token to add to the url.
For the hook to change the default email sent by Wordpress, you can use wp_new_user_notification_email that is available since the 4.9 of Wordpress.
In the function itself you could do something like :
function wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname) {
// Generate the token (there is other function available with php 7, but this one works great)
$token = bin2hex(openssl_random_pseudo_bytes(16));
// Add the token to the user
update_user_meta( $user->id, 'confirm_email_token', $token );
// Get your login url
$log_in_url = wp_login_url();
// Add user id and token to the url
$url = add_query_arg(
array(
'token' => $token,
'user_id' => $user->id
),
$log_in_url
);
//
$wp_new_user_notification_email['subject'] = 'Welcome on our website, please confirm your email';
$wp_new_user_notification_email['message'] = 'Blablabla... the url to confirm is: '. $url;
return $wp_new_user_notification_email;
}
6. Hook on the login page to check the $_GET, looking for user_id and token.
Here we check the token and the user. If everything is okay, update the user meta has_confirm_email to 1, so the user can connect, and add a message : "Your email has been confirmed, you can now log in"
add_action( 'login_init', 'custom_login_init');
function custom_login_init(){
if(!empty($_GET['token']) && !empty($_GET['user_id'])) {
if(get_the_author_meta( 'confirm_email_token', $_GET['user_id']) === $_GET['token']) {
// Set the has_confirm_email to 1 so the user can now log in
update_user_meta( $user_id, 'has_confirm_email', 1);
update_user_meta( $user_id, 'confirm_email_token', '');
echo 'Your email has been confirmed, you can now log in';
}
}
}
7. Time for thinking
Okay, after all of his, i'm gonna think a little, and read what i have tell you, to check if there is no mistake ^^. Tell me if you need more explanations.
I think this is a good start for you, and if you find the right hooks, you will achieve this rapidly.
Be careful on some hooks that i have used, because your theme may have use a custom registration or something.
Here is what I did:
added a column in the table wp_users to receive the email confirmation code
built a plugin (details here) called user-emails that allows me to bypass the first email sent upon registration by redefining the function wp_new_user_notification (in which I generate the confirmation code, add it to the user in the DB and send a confirmation email of my own sauce)
redefined the wp_authenticate function inside the same plugin user-emails to allow me to check if the email has been confirmed (column value not null)
created a page for the confirmation with the email and code passed to it that, in case of success, display a message and a link to the home page in order to login
finally got my hands on that one tiny line of code responsible for the auto login after registration located in the page user_auth.php inside the theme folder itself (that file also contains the layout for the login and registration form)
wp_set_auth_cookie( $user_id, true, $secure_cookie );
made sure to display a message after registration informing the user to check his email for the confirmation email

Print some data before sending email (Contact form 7)

I use in a CMS wordpress website form (Contact Form 7) for recording the patients. The need arose to after clicking the button to sign in before sending email has printed some coupon with the user-entered data in a pre-prepared template.
How to implement it? Please help.
screen http://pastenow.ru/2EQF9
What i get from your question is
So before sending mail, you need to show which data is sending. for this, you can use the hook
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
function wpcf7_add_text_to_mail_body($contact_form){
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
print_r($posted_data)
}
}

Hide empty fields in email - Contact Form 7

I've spend almost the whole day looking for an answer, but nothing helped..
I made a large form with Contact Form 7, but parts of the form will hide, depends on your choice. For example, if you choose '2 persons', there will two parts show up.
But, if I fill in the fields for one person (so the other fields are empty and hidden), the fields will visible in the email. I only want to see the filled fields in the email.
I'm sorry if I'm a bit unclear. Could use some help, please.
Solved!
I found a solution by myself, which was already in Contact Form 7. The fields in the e-mail were not on the same line, so when I checked "Exclude lines with blank mail-tags from output", nothing happend. I've put it all on the same line and now it works.
Actually you need to implement your own custom email body component with,
add_filter('wpcf7_mail_components','my_custom_mail', 10,2);
function my_custom_mail($mail_component, $contact_form){
$mail_component['subject']; //email subject
$mail_component['sender']; //sender field (from)
$mail_component['body']; //email body
$mail_component['recipient']; //email recipient (to)
$mail_component['additional_headers']; //email headers, cc:, bcc:, reply-to:
$mail_component['attachments']; //file attachments if any
$key_values = array();
$tags = $contact_form->scan_form_tags(); //get your form tags
foreach($tags as $tag){
$field_name = $tag['name'];
if(isset($_POST[$field_name]) && !empty($_POST[$field_name])){
//get all the submitted fields form your form
$key_values[$field_name] = $_POST[$field_name];
}
}
//you have all the submitted field-name => value pairs in the array $key_values
//you can now reset you email body
$body = "Dear ".$key_values['your-name'].',';
...
$mail_component['body'] = $body;
return $mail_component;
}
To do this you can use the free Plug-in Conditional Fields for Contact Form 7 by Jules Colle.
With it you can create groups of information.
Then you create a checkbox and in the settings you choose which group will appear when it's selected.
Try adding this to your functions.php file:
add_filter( 'wpcf7_mail_components', 'remove_blank_lines' );
function remove_blank_lines( $mail ) {
if ( is_array( $mail ) && ! empty( $mail['body'] ) )
$mail['body'] = preg_replace( '|\n\s*\n|', "\n\n", $mail['body'] );
return $mail;
}
I have found the snippet here: https://wordpress.org/support/topic/plugin-contact-form-7-how-to-do-away-with-blank-lines-in-email-for-unfilled-form-items/ Depending on how you set up your email this might not work as this removes just the empty lines. Let me know if this works for you, and if not please provide the code from your email body, and an example email sent would be nice too.

Resources