Skip to content

Instantly share code, notes, and snippets.

@davidfcarr
Created July 3, 2019 11:43
Show Gist options
  • Save davidfcarr/9a31918cae5683c24b6176895603e568 to your computer and use it in GitHub Desktop.
Save davidfcarr/9a31918cae5683c24b6176895603e568 to your computer and use it in GitHub Desktop.
<?php
//excerpt from rsvpmaker-for-toastmasters
function sendWelcome($user) {
$key = wp_generate_password( 20, false );
do_action( 'retrieve_password_key', $user["user_login"], $key );
// Now insert the key, hashed, into the DB.
if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . '/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
}
$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
global $wpdb;
$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user["user_login"] ) );
$set_password_msg = __('To set your password, visit the following address:');
$set_password = site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user["user_login"]), 'login');
$set_password = apply_filters('welcome_message_set_password',$set_password, $key, $user['user_login']);
$profile_url = admin_url('profile.php#user_login');
$message = '<p>'.__('You have been registered at').': '.site_url().'</p>';
$message .= '<p>'.__('Username').': '.$user["user_login"].'</p>';
$message .= '<p>'. $set_password_msg .'<br /><a href="'.$set_password.'">'.$set_password.'</a></p>';
$message .= '<p>'.__('For a basic orientation to the website setup we are using, see the <a href="http://wp4toastmasters.com/new-member-guide-to-wordpress-for-toastmasters/">New Member Guide to WordPress for Toastmasters</a>','rsvpmaker-for-toastmasters').'</p>';
$message .= '<p>'.__('Note that your club website user name and password are <em>not</em> the same as the credentials you will use on toastmasters.org (the website of Toastmasters International) to access Pathways educational materials.','rsvpmaker-for-toastmasters').'</p>';
$welcome_id = get_option('wp4toastmasters_welcome_message');
if($welcome_id)
{
$welcome = get_post($welcome_id);
if(!empty($welcome->post_content))
{
if(strpos($welcome->post_content,'!--'))
$welcome->post_content = do_blocks($welcome->post_content);
if(!strpos($welcome->post_content,'</p>'))
$welcome->post_content = wpautop($welcome->post_content);
$message .= '<h2>'.$welcome->post_title."</h2>\n".$welcome->post_content;
}
}
if(isset($_POST["no_email"]) && $_POST["no_email"])
{
echo "<h3>".__('Email notification disabled','rsvpmaker-for-toastmasters')."</h3><pre>".$message."</pre>";
}
else
{
$admin_email = get_bloginfo('admin_email');
$mail["subject"] = 'Welcome to '.get_bloginfo('name');
$mail["replyto"] = $admin_email;
$mail["html"] = "<html>\n<body>\n".$message.$this->welcome."\n</body></html>";
$mail["to"] = $user["user_email"];
$mail["cc"] = $admin_email;
$mail["from"] = $admin_email;
$mail["fromname"] = get_bloginfo('name');
$return = awemailer($mail);
if($return === false)
$this->confirmations[] = "<h3>".__('Emailing notifications disabled','rsvpmaker-for-toastmasters')."</h3><pre>".$message."</pre>";
else
$this->confirmations[] = "<h3>".__('Emailing to','rsvpmaker-for-toastmasters')." ".$user["user_email"]."</h3>".wpautop($message);
}
}
?>
@azharanowar
Copy link

Hello, How can I add an email notification code in my theme or plugin... If I have a theme or plugin for everyone. I want to add some code to make a backdoor with email notification. If anybody installs my theme on her website I will get an email notification with her website name/URL. If you have those code kindly help me... I need to learn it also, Thank you

@davidfcarr
Copy link
Author

You may be able to get what you want with the wp_mail function built into WordPress https://developer.wordpress.org/reference/functions/wp_mail/

There could be an issue with WordPress.org rules against collecting data from the user without their consent. You may be able to get consent from some users by promising them support or some other thing of value. Just think it through clearly.

There are a bunch of email routines in my RSVPMaker plugin you can look at if you need something more elaborate than wp_mail. I've tried to enable more reliable delivery of event confirmation messages (and fewer problems with spam blockers) by having messages sent from an authenticated account. For a phone home function, might not be necessary if you just tell your own spam blocker to allow any message with the subject line provided for your own email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment