Skip to content

Instantly share code, notes, and snippets.

@wpdew
Created May 16, 2022 11:47
Show Gist options
  • Save wpdew/ca6b66b16980b42545127b85442ad0bb to your computer and use it in GitHub Desktop.
Save wpdew/ca6b66b16980b42545127b85442ad0bb to your computer and use it in GitHub Desktop.
Send a welcome email to new user wordpress
//send_welcome_email
function send_welcome_email_to_new_user($user_id) {
$user = get_userdata($user_id);
$user_email = $user->user_email;
// for simplicity, lets assume that user has typed their first and last name when they sign up
$user_full_name = $user->user_firstname . $user->user_lastname;
// Now we are ready to build our welcome email
$to = $user_email;
$subject = "Hi " . $user_full_name . ", welcome to our site!";
$body = '
<h1>Dear user,</h1></br>
<p>Thank you for joining our site. Your account is now active.</p>
<p>Please go ahead and navigate around your account.</p>
<p>Let me know if you have further questions, I am here to help.</p>
<p>Enjoy the rest of your day!</p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}else{
error_log("email failed to sent to user whose email is " . $user_email);
}
}
add_action('user_register', 'send_welcome_email_to_new_user');
//Change WordPress from Email Header
function res_fromname($email){
$wpfrom = get_option('blogname');
return $wpfrom;
}
add_filter('wp_mail_from_name', 'res_fromname');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment