TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

March 28, 2024, 08:34:38 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,104
  • Total Topics: 21,212
  • Online today: 174
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 165
  • Total: 165

[Block] Periodic Emailer - for New Registered Users

Started by Thurnok, February 12, 2007, 09:42:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thurnok

Block Type: phpblock
Block Location: Left/Right/Center/Frontpage/Article Block
Description:
This block allows you to send custom periodic emails to newly registered users.  You can send any custom text or HTML email you design from a template.  There are a number of replacable parameters you can use (tags) to fill in certain user specific and board specific information in your template as well as the Subject of the email.  You can also use multiple email message and subject templates and have them sent in sequence or randomly.  You determine how long you want to send periodic emails to a member and what intervals to send them.

This block creates its own table if the table does not yet exist, and does not alter any existing SMF or TP tables.  It also requires you to actually create at least one email template file (text or HTML) and place it somewhere on your site under your forum tree structure.  No HTML syntax checking is performed, so you need to make a working HTML file first - I'm not a miracle worker ya know! ;)

It is recommended you create a block without a title or frame so that it is invisible to a user.  Admins however, will get a short blurb about the number of msgs just sent, and if any had errors.  This code will probably work best as a block that is enabled on your frontpage to registered users only.

I created this block as a solution to a request on TinyPortal.net.

Config Variables:
The following are the only the most important user configurable variables.  For info on all configurable variables see the comments in the actual code.  Most things you can leave as the default, but you will need to definately set up the template files array and ensure you upload your template files.

$nrpe_send_expire = 12;  ::  Once a member has been registered for this many days, they will no longer receive the periodic emails

$nrpe_periodic_rate = 3;  ::  Every this many days, they get a new email

The following several lines are the TAG variables.  You can certainly change the TAGs if you like, but I'd suggest leaving them as is unless you have a specific reason or need to change them.  The main reason they are located in the "Admin Configuration Area" is so you can see what they are for use in your email message (and subject).  The parts in green below are what you put in your actual text or HTML file and it gets replaced by the SMF information they refer to before sending the email to the member.
$nrpe_memberName_tag = '<<{memberName}>>';
$nrpe_realName_tag = '<<{realName}>>';
$nrpe_dateRegistered_tag = '<<{dateRegistered}>>';
$nrpe_posts_tag = '<<{posts}>>';
$nrpe_lastLogin_tag = '<<{lastLogin}>>';
$nrpe_unreadMessages_tag = '<<{unreadMessages}>>';
$nrpe_siteSignature_tag = '<<{siteSignature}>>';
$nrpe_site_name_tag = '<<{forumName}>>';


Template files array, add as many files as you want, for ease of reading, put
each one on a seperate line within the array().  Make sure you put a comma after
each closing single quote except for the last file you list.
These files must be under your forum directory somewhere.  They can be in any
subdirectory under your forum root, or the forum root itself.  If in the forum
root, just use the filename, otherwise use somedir/filename for each array element.
EXAMPLE:
$nrpe_email_files = array(
   'mycoolhtmlemail.html',
   'textemails/mytextemail.txt',
   'otherdir/stuff/morestuff/some_emails/thisemail.htm'
);

This is set up to use the example template file I included below.
$nrpe_email_files = array(
   'email_template.txt'
);

array of subjects - match them to your array of email files!  (notice you can use Tags here too!)
if you do not have enough subjects in your array to match email files array, first subject array element is used for each remaining email file.
$nrpe_email_subjects = array(
   'Hi <<{realName}>>, we here at "<<{forumName}>>" wish to welcome you!'
);

$nrpe_email_signature = '<<{forumName}>> Management';  ::  The signature to place at end of the email msg

$nrpe_filemode = 0;  ::  template file mode (0 = in order, 1 = random)


First, here's an example email template file you can use for testing purposes on your own site.  If you name this file email_template.txt and place it in your forum root, you can then leave the template file array at the default to actually test using this file.  If you name it something different or place it in a subdirectory somewhere, update the template file array to reflect your changes.

Hi <<{memberName}>>!

I just wanted to thank you for registering at <<{forumName}>> on <<{dateRegistered}>>!
We really appreciate you coming by to check us out!

Please come back and visit us soon!

<<{siteSignature}>>


And here is the actual block code:

//////////////////////////////////////////////
// New Registration Periodic Emailer Version 1.0
// Developed by Thurnok
// thurnok -AT- tinyport .DOT. net
// Complete Computer Services
// February 11, 2007
//
// This is a php block and/or php Article snippet.  It works in any block
// position (left/right/center/frontpage/article).
// It will parse list of newly registered users (number of days you specify)
// and send them a periodic email (number of days you specify).
//
//////////////////////////////////////////////

/*
****************************************
****************************************
*** !! User Configuration Section !! ***
****************************************
****************************************
*/

// the New Registration Periodic Emailer table name
$nrpe_tablename = "nrpe";

// expiration for number of days to send the periodic email
$nrpe_send_expire = 12;

// periodic rate (days between sends)
$nrpe_periodic_rate = 3;

// tags for your text or html template file - replaced with SMF info
$nrpe_memberName_tag = '<<{memberName}>>';
$nrpe_realName_tag = '<<{realName}>>';
$nrpe_dateRegistered_tag = '<<{dateRegistered}>>';
$nrpe_posts_tag = '<<{posts}>>';
$nrpe_lastLogin_tag = '<<{lastLogin}>>';
$nrpe_unreadMessages_tag = '<<{unreadMessages}>>';
$nrpe_siteSignature_tag = '<<{siteSignature}>>';
$nrpe_site_name_tag = '<<{forumName}>>';

// Template files array, add as many files as you want, for ease of reading, put
// each one on a seperate line within the array().  Make sure you put a comma after
// each closing single quote except for the last file you list.
// These files must be under your forum directory somewhere.  They can be in any
// subdirectory under your forum root, or the forum root itself.  If in the forum
// root, just use the filename, otherwise use somedir/filename for each array element.
// EXAMPLE:
/*
$nrpe_email_files = array(
'mycoolhtmlemail.html',
'textemails/mytextemail.txt',
'otherdir/stuff/morestuff/some_emails/thisemail.htm'
);
*/
$nrpe_email_files = array(
'email_template.txt'
);

// array of subjects - match them to your array of email files!  (notice you can use Tags here too!)
// if you do not have enough subjects in your array to match email files array, first subject array element is used
$nrpe_email_subjects = array(
'Hi <<{realName}>>, we here at "<<{forumName}>>" wish to welcome you!'
);

// The signature to place at end of the email msg
$nrpe_email_signature = '<<{forumName}>> Management';

// template file mode (0 = in order, 1 = random)
$nrpe_filemode = 0;

/*
****************************************
****************************************
*/

//////////////////////////////////////////////
//
// The rest of this you should leave as is
// unless you are overly industrious :)
//
//////////////////////////////////////////////

// globals for database vars / site vars
global $db_prefix, $tp_prefix, $boarddir, $webmaster_email;
// globals for user information
global $context, $user_info, $ID_MEMBER;

// fix for TP 0.8.6 and lower
if (empty($tp_prefix)){
$tp_prefix = $settings['tp_prefix'];
}

define('NRPE_TABLE', $tp_prefix.$nrpe_tablename);

/*
****************************************
****************************************
***         !! Functions !!          ***
****************************************
****************************************
*/
if (!function_exists("stripos")){
function stripos($haystack, $needle, $offset = 0){
return strpos(strtolower($haystack), strtolower($needle), $offset);
}
}


/*
****************************************
****************************************
***     !! Main Code Section !!      ***
****************************************
****************************************
*/

// setup our time vars
$nrpe_curtime = time();
$nrpe_expire_time = $nrpe_curtime - ($nrpe_send_expire * 24 * 60 * 60);
$nrpe_periodic_time = $nrpe_curtime - ($nrpe_periodic_rate * 24 * 60 * 60);

// make sure admin put in at least one file and one subject
$nrpe_numfiles = count($nrpe_email_files);
$nrpe_numsubjs = count($nrpe_email_subjects);
if ($nrpe_numfiles < 1)
die('You need to specify at least one email file!');
if ($nrpe_numsubjs < 1)
die('You need to specify at least one email subject!');

// create our table if it does not already exist
$dbresult = mysql_query("DESCRIBE ".NRPE_TABLE);
if (!$dbresult){
if (mysql_errno() == 1146){
// table doesn't exist, create it
if (!mysql_query('CREATE table '.NRPE_TABLE.' (id MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY, date_sent INT UNSIGNED NOT NULL, msg_index TINYINT UNSIGNED NOT NULL);'))
die('Cannot create table '.NRPE_TABLE.'!');
} else {
die('MySQL Error: '.mysql_error());
}
}
mysql_free_result($dbresult);

// remove rows that have expired
$dbquery = 'DELETE '.NRPE_TABLE.' FROM '.NRPE_TABLE.', '.$db_prefix.'members WHERE '.NRPE_TABLE.'.id = '.$db_prefix.'members.ID_MEMBER AND '.$db_prefix.'members.dateRegistered < '.$nrpe_expire_time;
db_query($dbquery, __FILE__, __LINE__);

// add new member ids if they qualify
$dbquery = 'INSERT INTO '.NRPE_TABLE.'(id) SELECT s.ID_MEMBER FROM '.$db_prefix.'members AS s LEFT JOIN '.NRPE_TABLE.' AS n ON s.ID_MEMBER = n.id WHERE n.id IS NULL AND s.dateRegistered >= '.$nrpe_expire_time;
db_query($dbquery, __FILE__, __LINE__);

// get list of who to SEND to, along with TAG info
$nrpe_sendto = array();
$dbquery = 'SELECT n.*, s.memberName, s.realName, s.dateRegistered, s.posts, s.lastLogin, s.unreadMessages, s.emailAddress FROM '.NRPE_TABLE.' AS n, '.$db_prefix.'members AS s WHERE n.id = s.ID_MEMBER AND n.date_sent < '.$nrpe_periodic_time;
$dbresult = db_query($dbquery, __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($dbresult)){
$nrpe_sendto[] = $row;
}

$nrpe_total_msgs = mysql_num_rows($dbresult);
mysql_free_result($dbresult);

// support for php < 4.2.0
if (!empty($nrpe_filemode)) srand((double) microtime() * 1000000);

// create our search array
$nrpe_search_array = array(
$nrpe_memberName_tag,
$nrpe_realName_tag,
$nrpe_dateRegistered_tag,
$nrpe_posts_tag,
$nrpe_lastLogin_tag,
$nrpe_unreadMessages_tag,
$nrpe_siteSignature_tag,
$nrpe_site_name_tag
);

$nrpe_email_errors = 0;
// iterate thru each eligible member
foreach ($nrpe_sendto as $nrpe){
// get our index - either next in sequence, or random, depending on mode
$nrpe_index = empty($nrpe_filemode) ? $nrpe['msg_index'] : rand(0, $nrpe_numfiles - 1);
// if past end (shouldn't happen here, but might as well), set back to 0
if ($nrpe_index >= $nrpe_numfiles) $nrpe_index = 0;
// get the file and subject for this user (slower method for compatibility with php < 4.3.0)
$nrpe_handle = fopen($boarddir . '/' . $nrpe_email_files[$nrpe_index], "r");
$nrpe_a_msg = fread($nrpe_handle, filesize($boarddir . '/' . $nrpe_email_files[$nrpe_index]));
fclose($nrpe_handle);
$nrpe_a_subj = $nrpe_index < $nrpe_numsubjs ? $nrpe_email_subjects[$nrpe_index] : $nrpe_email_subjects[0];

// get our replace array (each member's is different except for the forum site name, which is why it is here)
$nrpe_replace_array = array(
$nrpe['memberName'],
$nrpe['realName'],
date('F d, Y', $nrpe['dateRegistered']),
$nrpe['posts'],
date('F d, Y \at h:i:s A', $nrpe['lastLogin']),
$nrpe['unreadMessages'],
$nrpe_email_signature,
$context['forum_name']
);

// parse tags from subj and file
$nrpe_a_subj = str_replace($nrpe_search_array, $nrpe_replace_array, $nrpe_a_subj);
$nrpe_a_msg = str_replace($nrpe_search_array, $nrpe_replace_array, $nrpe_a_msg);

// update this user's row in our table
if (empty($nrpe_filemode)){
// using sequence, so set index to next in sequence, or begining if at end
$nrpe_index++;
if ($nrpe_index >= $nrpe_numfiles) $nrpe_index = 0;
}
$dbquery = 'UPDATE '.NRPE_TABLE.' SET date_sent = '.$nrpe_curtime.', msg_index = '.$nrpe_index.' WHERE id = '.$nrpe['id'];
db_query($dbquery, __FILE__, __LINE__);

// get the msg headers ready
$headers = '';
// is it an HTML msg?
if (stripos($nrpe_a_msg, "<html>") !== false){
// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
}
// Additional headers
$headers .= 'From: '.$webmaster_email."\r\n";
$headers .= 'Reply-To: '.$webmaster_email."\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";

// send the email to them
if (!mail($nrpe['emailAddress'], $nrpe_a_subj, $nrpe_a_msg, $headers)) $nrpe_email_errors++;
}

// notify admins how many users are to be sent msgs
if ($context['user']['is_admin']){
echo '<hr />';
echo '<b>New Registered Users Periodic Emailer</b><br />';
echo 'Number of msgs to send = '.$nrpe_total_msgs.'<br />';
echo 'Message Errors = '.$nrpe_email_errors.'<br />';
echo '<hr />';
}

Ken.

This has the look of being a great bit of code! :up:
I'm going to start working on the block later today and get it running on my sites.
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Ken.

This code works as advertised!
In my case it will take some days to get it setup the way I want because there will be a different email for each of four mailings and they will have pictures, or graphics included. Plus, Iââ,¬â,,¢m doing this for two different sites.

One issue for me, or anyone who uses an editor like MS FrontPage, is making sure that any image files that are included in the email are linked with the full path, same goes for any hyperlinks. F/P wants to shorten any link as much as possible, so if Iââ,¬â,,¢m not careful to insert the full link for the image it wonââ,¬â,,¢t show at the email delivery point.

You can see a screen shot of one of my test emails here if you likeââ,¬Â¦ the quality is not very good, but it kind of gives you an idea of where Iââ,¬â,,¢m trying to go. The email looks much better than the S/S.
Screen Shot

Thanks Thurnok... looks like you have come up with another hit!  ;D
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Thurnok

Welcome.  :)

Yes, definately use full URLs for your images and links, since the recipient will not have a baseurl of your site when reading their email.

... enjoy.


Assistance

$nrpe_send_expire 12 ::  Once a member has been registered for this many days, they will no longer receive the periodic emails

can that be reversed?
to say ::only send to those that have been registered for XX days, if before that, they do not get the email.

Thurnok

That was actually something I planned to add, just haven't gotten around to it.  Been pretty tied up the past 3+ weeks (including weekends) for work.

So pretty much just slipped my mind originally, but I will put it in.  Pretty simple addition.

Assistance

can we get more Variables for who the email will be sent out too?

group #
post count + - # (above or below #)
registered before or after a date (input like adding event to calendar to select)

Thurnok


wilsy

This would be great if it could be adapted to another few of blocks: -

Send emails to members who havent logged in for XX days
$nrpe_periodic_rate = 3;  ::  Every this many days, they get a new email

Send email to members who haven't made a post yet
$nrpe_periodic_rate = 3;  ::  Every this many days, they get a new email

Send email to members who havent posted in XX days
$nrpe_periodic_rate = 3;  ::  Every this many days, they get a new email

Regards,

Wilsy.