How to Send Out a Mass Email Using PHP

Posted in php by ShortLikeAFox on September 19th, 2008

So you want to send out a mass email or you want to create a program that can quickly be changed to send out multiple mass emails. PHP makes this easy. In this example I will assume that you have a list of email addresses you want to send a certain email to. I will also assume that you are keeping this list in a database, but this code could easily be adjusted to use either a hardcoded email address list or list from another source.

<?php

 

//First connect to the database that contains the email address information.
$user = "USER";
$host = "HOST";
$password = "PASSWORD";
$database = "DATABASE";

$cxn = mysql_connect($host, $user, $password) or die ("Couldn’t connect to server");
mysql_select_db($database);

//Query the table that contains the email addresses. Fill in your own table name here…
$query = "SELECT * FROM theEmailAddresses ";
$result = mysql_query($query, $cxn) or die (mysql_error($cxn));
$nrows = mysql_num_rows($result);

//Who the sender will be identified as. You can put what you want here, but it really isn’t too ethical to place an email address that you don’t control here.
$from = "From: me@mydomain.com";
//A standard email subject line
$subject = "What’s new at mydomain.com ";
//The message
$message = "Hi valued friend,

We now sell cookies at mydomain.com. Be sure to check it out!
Sincerely,
me"
;

//We’ve already queried all of the email address. Now we just need to send the email
for ($i = 0; $i <$nrows; $i++){

$row = mysql_fetch_assoc($result);
extract($row);
//Replace $email with whatever the column of email addresses is called
$to = "$email";
//Send the email to each email in the database
if(mail($to, $subject, $message, $from)){

//Print the name of emails that were successfully sent. I use this just to make sure that the program hasn’t frozen. In theory their should be a steady stream of names being printed>

echo("$to<br/>");

}

}




?>

This bit of code takes advantage of the php mail() function. This is a very powerful and easy to use function. Remember that with great power comes great responsibility. Try not to use this function for evil.

How to Manually Insert a User Into an SMF Forum Using PHP

Posted in SMF (Simple Machine Forum), php by ShortLikeAFox on July 28th, 2008

This specific example is for SMF 1.1.5. I can’t guarantee it will work with any other version.

So you run Simple Machine Forum Software and want to manually enter a user? No problem. I know this problem seems very specific, but the ideas explained here can be adapted to other types of forums and other database driven software packages such as Wordpress. Before we get into exactly how to do this, let’s take a look at how Simple Machine Forums keeps track of users. In the SMF database there is a table called smf_members. The smf_members structure looks like this:

Field Type Null

Key Default Extra
ID_MEMBER mediumint(8) unsigned NO PRI NULL auto_increment
memberName varchar(80) NO MUL    
dateRegistered int(10) unsigned NO MUL 0  
posts mediumint(8) unsigned NO MUL 0  
ID_GROUP smallint(5) unsigned NO MUL 0  
lngfile tinytext NO MUL    
lastLogin int(10) unsigned NO MUL 0  
realName tinytext NO      
instantMessages smallint(5) NO   0  
unreadMessages smallint(5) NO   0  
buddy_list text NO      
pm_ignore_list text NO      
messageLabels text NO      
passwd varchar(64) NO      
emailAddress tinytext NO      
personalText tinytext NO      
gender tinyint(4) unsigned NO   0  
birthdate date NO MUL 0001-01-01  
websiteTitle tinytext NO      
websiteUrl tinytext NO      
location tinytext NO      
ICQ tinytext NO      
AIM varchar(16) NO      
YIM varchar(32) NO      
MSN tinytext NO      
hideEmail tinyint(4) NO   0  
showOnline tinyint(4) NO   1  
timeFormat varchar(80) NO      
signature text NO      
timeOffset float NO   0  
avatar tinytext NO      
pm_email_notify tinyint(4) NO   0  
karmaBad smallint(5) unsigned NO   0  
karmaGood smallint(5) unsigned NO   0  
usertitle tinytext NO      
notifyAnnouncements tinyint(4) NO   1  
notifyOnce tinyint(4) NO   1  
notifySendBody tinyint(4) NO   0  
notifyTypes tinyint(4) NO   2  
memberIP tinytext NO      
memberIP2 tinytext NO      
secretQuestion tinytext NO      
secretAnswer varchar(64) NO      
ID_THEME tinyint(4) unsigned NO   0  
is_activated tinyint(3) unsigned NO   1  
validation_code varchar(10) NO      
ID_MSG_LAST_VISIT int(10) unsigned NO   0  
additionalGroups tinytext NO      
smileySet varchar(48) NO      
ID_POST_GROUP smallint(5) unsigned NO MUL 0  
totalTimeLoggedIn int(10) unsigned NO   0  
passwordSalt varchar(5) NO      

Fields we need to pay attention to:

  • memberName - Self Explanitory.
  • dateRegistered - Isn’t neccessary, but if not filled out the date registered displays as December 31st 1969. The date is saved as an epoch timestamp. Don’t know how to calculate timestamps in your head? No problem. I use the free generator found here.
  • realName - Should be inserted. When I’m not sure I just repeat memberName here.
  • emailAddress - Self Explanitory.
  • is_activated - Must be set to 1 since we are manually activating a member.
  • ID_POST_GROUP - I won’t lie. I’m not sure what this is, but it always seems to be set to 4. So…. I always set it to 4. Not the best way to program, but what can you do?
  • passwd - The password you want to give the new user. It is impossible to figure out how to enter without looking at the SMF documentation. The proper code to encrypt a SMF password for the database looks like this:

$passwd = sha1(strtolower($memberName).$password)

In the above line of code, $password is the user’s actual password.

How to Do It:

//First, connect to the SMF database

$host="hostname"; // Host name
$username="username"; // Mysql username
$mypassword="password"; // Mysql password
$db_name="username"; // Database name

mysql_connect("$host", "$username", "$mypassword")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$memberName = "ironMan";
$realName = "Tony Stark";
$emailAddress = "ironMan@ironMan.com";
$is_activated = 1;
$ID_POST_GROUP = 4;

$password = “Tony1234″;

$password = sha1(strtolower($memberName).$password);

$dateRegistered = 1216951200; // 7-25-2008 2 AM

$query = "INSERT INTO smf_members(memberName, realName, emailAddress, is_activated, ID_POST_GROUP, passwd, dateRegistered) VALUES(’$memberName’, ‘$realName’, ‘$emailAddress’, ‘$is_activated’, ‘$ID_POST_GROUP’, ‘$password’, ‘$dateRegistered’)";
$result = mysql_query($query) or die ("SMF Error 101.234 ");

That’s all there is to it!