Hello I am using WP Job manager and I want to send an email to on the application email once the job listing has been approved by the admin. I have found this code https://wpjobmanager.com/document/tutorial-send-email-employer-job-listing-approved/ but this only works to send a notification email to a user that already has an account. I want to send an email to the email provided in the job listing as often those users do not have an account on our website. I am sorry if this question has already been answered, I have looked around and couldn't find one. Also I tried to tag this with WPJOBMANAGER but I don't have enough rep to create a tag.
Thanks for all your help.
Okay after doing a bunch of research I have managed to find a way to do this.
function listing_published_send_email($post_id) {
if( 'job_listing' != get_post_type( $post_id ) ) {
return;
}
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$job = get_the_job_application_method($post);
// Message to be sent to users.
$message = "Put the message you want to send here";
// Checks to see if the person who submitted the job listing is a user or a guest.
if ($author == 0 OR $author == NULL){
// Checks to see if the email is valid.
if ($job->type == 'email') {
$email = $job->email;
wp_mail($email, "Your job listing is online", $message);
}
else { // The email is not valid so return.
return;
}
}
else {
wp_mail($author->user_email, "Your job listing is online", $message);
}
}
add_action('pending_to_publish', 'listing_published_send_email');
add_action('pending_payment_to_publish', 'listing_published_send_email');
Should just be able to put this code in functions.php file and it will work.
Related
I'm building a members style site using WordPress. When a user registers their default role is "subscriber" once we manually approve their account we change the user role to "private_event_member", we need to send the user an email to tell them we have changed their role. I found the following code snippet and added it to the functions.php file
function user_role_update( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);
This failed to send the email as expected. So to be sure I decided to install a plugin called WP Mail Log and then also WP Mail SMTP, I configured the Sendblue SMTP option. I've tested this and all other emails like for example user registration notifications and new orders are being sent and recorded successfully in the logs, these are being received. The above mentioned code however seems to do nothing.
This seems to be a widely used piece of code that should work so can anyone explain to me why this snippet behaves differently from other mail on the server? It doesn't even appear in the sending logs so as far as I can see it's not doing anything at all. Has the set_user_role action I'm hooking into changed? What could be the cause?
Any help much appreciate!
You should use profile_update hook for this. Read more here - https://developer.wordpress.org/reference/hooks/profile_update/
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($userdata,true)); // debug
//If we change role send email
if($old_role != $userdata['role']):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);
Send notification only if you change to specific user role
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($old_role,true)); // debug
//If we change role send email
if($old_role != 'private_event_member' && $userdata['role'] == 'private_event_member'):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);
I am currently creating a website with Wordpress, I am creating my theme and I am not using jQuery. I need to introduce a simple contact form, which sends an email on submission and all plugins need jquery to work.
Is it safe to create a contact form that sends an email? Is there a risk of SQL injection since I do not query the database on submission?
I have very little security skill, any information or clarification will be welcome
So for example something like:
$name = "{$_POST['message_name']} {$_POST['message_lastname']}"; // I like to combine first and lastname in to 1 variable.
$email = $_POST['message_email'];
$website = $_POST['message_url'];
$message = $_POST['message_description'];
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
$response = form_validation_response( 'error', $email_invalid );
} else {
if ( empty( $name ) || empty( $message) ) {
$response = form_validation_response( 'error', $missing_content );
}
}
// The most simple check you can do is make sre that the fields are NOT empty.
The form_validation_response method is a simple function which you can use to return error message:
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
function form_validation_response( $type, $message ) {
$class = 'px-2 py-1 mb-6 rounded-md' // These are tailwind classes, but it could be bootstrap
if ( $type == 'success' ) {
$class .= "border border-green-800 text-green-700";
} else {
$class .= "border border-redish text-redish";
}
return "<div class='{$class}'>{$message}</div>";
}
The example above is used to validate the email, but you can also make sure that the fields are actually submitted, before even starting the validation process:
If you're not familiar with creating a "secure" php form I would advice you to use a plugin for this.
If your "allowed" to install plugins, then have a look "form plugins" like:
WPForms (https://wpforms.com/)
Gravity Forms (https://www.wpbeginner.com/refer/gravityforms/)
Contact Form 7 (https://wordpress.org/plugins/contact-form-7/)
This is just a few of the form plugins that are available. Depending on your need/budget you should then make a decision which plugin fits the best (some are free, freemium, premium etc.).
I am using a form to get newsletter sign ups on my website. I am using a contact.php file which works well but there is no validation so I occasionaly and sometimes frequently get blank responses.
I'm not sure why this is, but I believe I need validation.
This is my original code
<?php
/*
Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew
This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.
*/
$to = "hello#interzonestudio.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject
if(!isset($_GET['action']))
{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}
/* Now lets trim up the input before sending it */
$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address
mail($to,$subject,$message,"From: ".$email.""); //a very simple send
echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
?>
and this is the code I tried to add to validate but it doesnt work.
<?php
/*
Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew
This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.
*/
$to = "jcash1#gmail.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject
if(!isset($_GET['action']))
{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}
/* Now lets trim up the input before sending it */
$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address
/* Validation */
$error=0; // check up variable
$errormsg = '<ul class="errorlist">';
/* get it checking */
if(!check_email($email))
{
$errormsg.= "<li class='errormessage'>ERROR: not a valid email.</li>";
$error++;
}
$errormsg .= '</ul>';
if($error == 0) {
mail($to,$subject,$message,"From: ".$email.""); //a very simple send
echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
} else {
echo 'error|'. $errormsg;
}
?>
Can anyone offer some insight?
I cannot for the life of me get this to work...
I am getting an Error with the plugin and I have loaded it correctly
so I tried adding this :
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
//your email sending code here
} else {
echo("$email is not a valid email address");
}
like so:
<?php
/*
Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew
This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.
*/
$to = "hello#interzonestudio.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject
if(!isset($_GET['action']))
{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}
/* Now lets trim up the input before sending it */
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address
mail($to,$subject,$message,"From: ".$email.""); //a very simple send
echo 'contactarea|<div id="thanks">Thank you. We promise you won’t regret it.</div>'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
} else {
echo("$email is not a valid email address");
}
?>
Which is not working. I think it is beauce I have implemented the code in the wrong place but I am not sure. Any help would be greatly appreciated.
You can use filter_var() function in PHP for validating email addresses.
For simply validating email addresses in PHP you can use it like this,
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Valid email";
}
And your code can be improved like this.
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
mail($to,$subject,$message,"From: ".$email.""); //a very simple send
echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
}
else {
$errormsg.= "<li class='errormessage'>ERROR: not a valid email.</li>";
$error++;
echo '</ul> error|'. $errormsg;
}
If you want to know more about it, visit official PHP documentation page here : http://php.net/manual/en/filter.filters.validate.php
Or use jquery validation plugin. I highly recommend it.
Code will look similar to below
$( "#myform" ).validate({
rules: {
field: {
required: true,
email: true
}
}
});
You can use server side validation by using this code
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
//your email sending code here
} else {
echo("$email is not a valid email address");
}
I'm making a new site, and already have email confirmation code in place. But is there a way of making a "go to your inbox" link that is specific to the users' email provider? For example:
Go to live.com if it's a #live.com email
Go to yahoo.com if it's a #yahoo.com email
Some sort of if statement I'm guessing? Just some ideas would be good!
$email = $_POST['email'];
$ext = array('live.com','yahoo.com');
$domain = explode('#', $email);
if ( $domain[1] == $ext[0])
{
// Go to Live.com
}
if( $domain[1] == $ext[1])
{
//Go to Yahoo.com
}
with email is field name in registration form /subscribe form
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.