Email based on nicknames???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Email based on nicknames???

Post by wesnoel »

Ok what I have is a form that when submitted looks at an initials field and send the contents to an email associated to that persons initials.

I have the form built.

The email addresses are listed in a flat file as such:

JPS john@host.com
WFN wes@host.com

etc.....

Where do I begin?
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

1) Read the file into an associative array, where each entry has two elements: initials and e-mail.

2) Take the set of initials that you have.

3) Loop through the array. When you find a match between your input initials and the initials field, get the e-mail field, and use sendmail to send the e-mail.
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Ok that makes sence.

But...

I am very new to PHP and I might need a little bit of a better pointer.

I'm not looking for a full handout of code but maybe some sample to get me started in the right direction.

Wes/
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

Code: Select all

<?php

/* I've skipped the reading in of the file which has the email addresses in it and just set a static array */

$inputEmailAddress = $_REQUEST&#1111;'you_selected_this'];

/* You'll want to change this to read the values in from a file */
$myEmailAbbrs = array( 'JPS'=>'john@host.com' , 'WFN'=>'wes@host.com');

foreach ( $myEmailAbbrs AS $key => $value ) &#123;
   // Split the array into pieces the $key and the $key's $value
   // Then Check to see if the inputEmail that was passed in matches one of the key's in the array
   if ( $inputEmailAddress == $key ) &#123;
      // you found a match? Set the realEmail and break out of the loop
      $realEmail = $myEmailAbbrs&#1111;$key];
      break;
   &#125;
&#125;

if ( !empty( $realEmail ) ) &#123;
   // Did we find a match above?
   // Yeah, so mail $realEmail and send to a thankie page or something
   mail($realEmail , 'Subject' , 'Message' );
   header( 'Location: EmailConfPage.php');
   exit();
&#125; else &#123;
    // Didn't find a email , error out...
   header( 'Location: somethingMessedUpPage.php');
   exit();
&#125;

?>
Is what I believe Leviathan is talking about...
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Thanks Evil...

Let me hack through that for a bit.

I'll let you know how it turned out.
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Indeed, that's what I meant. I'm the kind of person who won't write a lot of code first until I know the person really needs it. Sorry if I seemed cryptic Wes, and good luck learning :)
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Oh no problem Leviathan

Thank you for puting it out there for me to learn.

I think It might work out.

I'll keep you posted
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You'd get problems with duplicate initials if there are a large number of users.

Valid initials could also be guessed pretty easily making this insecure without additional checks.

None of that would be relevant if you're just working on a test script.
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Actually it is for an in-office tracking system. only a few admin will have access to the page sending emails. I'm not to worried about it.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

when you do these kind of executions it's better to change the time out to something longer, by default is 300ms=30seconds
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

OK I have tried to get this to work but to no avail.

Anyone willing to help out? :oops:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could where you've gotten to with the code?

Mac
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

OK I have figured The nikcnames part and the sending mail part. I can even add a template to make it all look nice and pretty. The code below is all working fine .....

This is where I find the email address based on the nickname value passed from the form.

Code: Select all

<?php
// File Path
$CSVfile = "email_list.csv";

//data from form
$initials = $_POST['initials'];

// Default email address
$email = "fail@noone.com";


/// Retrieve name form information
$initials = ereg_replace("/","",$initials);
$initials = strtolower($initials);


// Open the file, and retrieve the contents
$fp = fopen($CSVfile,"r");
while ($data = fgetcsv($fp,500,",")){
if ($initials == strtolower($data[0])){
$email = trim(strtolower($data[1]));
break;
}
}

//Close csv file
fclose($fp);

?>
...and this is the part that sends the templated email...

Code: Select all

<?php

$recipient = "$email"; 
$subject = "Blue Slip Verrification"; 


$template = "blue_slip_template.php"; 
$fcontents = fopen( $template, "r" ); 
$message = fread( $fcontents, filesize( $template ) ); 
fclose ( $fcontents ); 
clearstatcache(); 

// Declare Headers 
$headers = "From: BlueSlip  "; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 


$formattedMSG = htmlentities($message); 
mail($recipient, $subject, $message, $headers);


?>
Most of this is just made up of script I have found on this forum.

All of that works fine. The only thing I cant get to work is getting all of the varibles into the template before it sends.

Any one know how to do this?

The template is structured like so.

Code: Select all

&lt;head&gt;


&lt;STYLE&gt;
 
body &#123; 
                margin: 10pt; 
		  color: #003366; 
		  font-family: arial;
		   font-size: 9px;
	     &#125;     
	     
.test &#123; 
            font-family: verdana;
            color: #DC143C;
            font-size: 11px; 
            &#125;   
            
.edit &#123; 
            border: 1px dashed #003366;
            font-size: 11px; 
            &#125;              
            
            
  &#125;
 &lt;/STYLE&gt;
 
&lt;/head&gt;


&lt;body&gt;
	
	
		&lt;table border="1" color= "#003366" cellpadding="6" cellspacing="0" bgcolor="#000000" &gt;
			&lt;tr height="19"&gt;
				&lt;td valign="top" bgcolor="white" height="19"class="edit"&gt;&lt;b&gt;&lt;font color="#003366"&gt;Service Provider:&lt;/font&gt;&lt;/b&gt;&lt;font size="3"&gt;&lt;b&gt; $fn&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td bgcolor="white" class="edit"&gt;
					&lt;table border="0" cellspacing="5" cellpadding="0" bgcolor="white" class="edit"&gt;
						&lt;tr&gt;
							&lt;td valign="bottom" bgcolor="white" width="132"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;MID#&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td align="left" valign="bottom" bgcolor="white" width="156"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Date&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td valign="bottom" bgcolor="white" width="85"&gt;&lt;font size="2" color="#003366"&gt;&lt;b&gt;Initials&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
							&lt;td valign="bottom" bgcolor="white" width="13"&gt;&lt;/td&gt;
							&lt;td valign="bottom" bgcolor="white"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Supplies&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td align="center" valign="bottom" bgcolor="white" width="50"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;QTY&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td bgcolor="white" width="132" class="test"&gt; $mid &lt;/td&gt;
							&lt;td align="left" bgcolor="white" width="156" class="test"&gt;$date&lt;/td&gt;
							&lt;td bgcolor="white" width="85" class="test"&gt;$initials&lt;/td&gt;
							&lt;td bgcolor="white" width="13" class="test"&gt;&lt;font size="2"&gt;1.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup1&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty1&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="3" valign="bottom" bgcolor="white" width="383"&gt;&lt;font size="2" color="#003366"&gt;&lt;b&gt;Business Name&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;2&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup2&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty2&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="3" bgcolor="white" width="383"class="test"&gt;$businessname&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;3.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup3&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty3&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="3" valign="bottom" bgcolor="white" width="383"&gt;&lt;font size="2" color="#003366"&gt;&lt;b&gt;Address&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;4.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup4&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty4&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="3" bgcolor="white" width="383" class="test"&gt;$address&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;5.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup5&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty5&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td valign="bottom" bgcolor="white" width="132"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;City&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td valign="bottom" bgcolor="white" width="156"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;State&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td valign="bottom" bgcolor="white" width="85"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Zip&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;6.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup6&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty6&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td bgcolor="white" width="132" class="test"&gt;$city&lt;/td&gt;
							&lt;td bgcolor="white" width="156" class="test"&gt;$state&lt;/td&gt;
							&lt;td bgcolor="white" width="85" class="test"&gt;$zip&lt;/td&gt;
							&lt;td bgcolor="white" width="13"&gt;&lt;font size="2"&gt;7.&lt;/font&gt;&lt;/td&gt;
							&lt;td nowrap bgcolor="white" class="test"&gt;$sup7&lt;/td&gt;
							&lt;td align="center" bgcolor="white" width="50" class="test"&gt;$qty7&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td valign="bottom" bgcolor="white" width="132"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Phone&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
							&lt;td colspan="5" valign="bottom" bgcolor="white"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Contact Name&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
						&lt;/tr&gt;
					&lt;tr&gt;
							&lt;td bgcolor="white" width="132" class="test"&gt;$phone&lt;/td&gt;
							&lt;td colspan="5" bgcolor="white" class="test"&gt;$contact&lt;/td&gt;
						&lt;/tr&gt;
					&lt;tr&gt;
							&lt;td colspan="6" valign="bottom" bgcolor="white"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Notes&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
						&lt;/tr&gt;
					&lt;tr&gt;
							&lt;td colspan="6" bgcolor="white" class="test"&gt;$notes&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="6" valign="bottom" bgcolor="white"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Shipping Notes&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan="6" bgcolor="white" class="test"&gt;$shippingnotes&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
						&lt;td colspan="6" bgcolor="white"&gt;&lt;b&gt;&lt;font size="2" color="#003366"&gt;Customer Relations Notes&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
					&lt;/tr&gt;
					&lt;tr&gt;
							&lt;td colspan="6" bgcolor="white" class="test"&gt;$cus_notes&lt;/td&gt;
						&lt;/tr&gt;
				&lt;/table&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;

&lt;/body&gt;
Thanks for all of your help by the way. I really do appreciate all of you.

Wes/
Post Reply