Manage email content from database for whole project

<?php
function babyboom_mail($to,$subject, $body_text){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: BabyBoomApps ' . "\r\n";
return mail($to, $subject, $body_text, $headers);
}

function replace_email_template_variables($uid,$mailid,$link = 0) { // This function replaces the variables in the Body text for the email
$user_info_row = mysql_fetch_array(mysql_query("SELECT * FROM `bb_users` WHERE `id` = ".$uid));
$mail_info_row = mysql_fetch_array(mysql_query("SELECT * FROM `bb_emails` WHERE `id` = ".$mailid));


//get user information
$name = $user_info_row['fname']." ".$user_info_row['lname'];
$email = $user_info_row['email'];
$user_name = $user_info_row['username'];
$usertype = $user_info_row['usertype'];
if($usertype = "f")
{
$type = "Facebook";
}
elseif($usertype = "l")
{
$type = "LinkedIn";
}
elseif($usertype = "t")
{
$type = "Twitter";
}
else
{
$type = "";
}
//get email content from database
$body_text = $mail_info_row['mail_content'];
$body_subject = $mail_info_row['subject'];

//replace user information in mail content
$body_text = eregi_replace('%link%', $link, $body_text);
$body_text = eregi_replace('%utype%', $type, $body_text);
$body_text = eregi_replace('%name%', $name, $body_text);
$body_text = eregi_replace('%email%', $email, $body_text);
$body_text = eregi_replace('%user_name%', $user_name, $body_text);
$body_text = "<pre>".$body_text."</pre>";
return babyboom_mail($email,$body_subject,$body_text);

}



save the file as mails.php
Now send the email using above file

include('includes/mails.php'); //include the above file 
replace_email_template_variables($outputuser['id'],3);//pass the user id and email id from database

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s