Apr 20, 2008

PHP Mail

PHP Mail

The example below shows how to send email.

$mailadd="test@mysite.com";
$mesage="This is a test of using PHP to send email.";
$headers .= "From: <" . $mailadd . ">\n";
$headers .= "X-Sender: <" . $mailadd . ">\n";
$headers .= "X-Mailer: PHP\n"; // mailer
$headers .= "Return-Path: <" . $mailadd . ">\n"; // Return path for errors 
$subject="Test of PHP Mail sending";
mail($email, $subject, $mesage, $headers);

The example below shows how to read email from a mailbox and check the contents of the subject line. In this case, someone has sent an email to the inbox that we are reading. If the email contains the string "update?str_ID" the code will determine this and get the ID number to be updated. It uses several string functions to do this, so this example is a good example of using string functions.

1  <?php
2  $mailadd="submissions@mysite.com
3  $mailpass="password";
4  $mailhand=imap_open("{localhost/pop3:110}INBOX", $mailadd, $mailpass);
5  if ($mailhand)
6  {
7    $msgqty=imap_num_msg($mailhand);  //get number of mail messages
8    for ($i2=1; $i2<=$msgqty; $i2++) //Read each message
9    {
10     $header=imap_header($mailhand,$i2);  //Get the message header
11     echo $header->subject . "<br>";
12     $hd=strtolower($header->subject); //Convert to lower case characters
13     if ($opstr=strstr($hd,"update?"))  //See if header contained the string "update?"
14     {
15       $tok = strtok ($opstr," ");
16       if ($opstr=strchr($tok,"?"))  //Returns the string from the "?" until the end
17       {
18         $tok=substr($opstr,1);  //Strips the "?", should now have id
19         $id=intval($tok); //Converts the ID number to an integer
20         $newflag=1;
21         $query1="update " . $mainsection . " set update1=" . $newflag . " where id = " . $id;
22         mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);
23         imap_delete($mailhand,$i2);
24         echo $id . " marked to be updated.";
25       }
26     }
27   }
28   imap_expunge($mailhand);  //delete marked messages
29   imap_close($mailhand);  //113
30 }
31 else
32 {
33  echo "can't connect: " . imap_last_error();
34 }
35 ?>

The list below is an explanation of the lines:

Line 2 - Sets the address of the mail box.
Line 3 - Gets the password needed to read the mailbox and make changes.
Line 4 - Opens the mailbox returning a handle much like a file handle.
Line 5, 6, 34 - Begins an if statement that only runs if the mailbox was opened successfully. This is because the returned value is NULL if the mailbox was not opened. Code between line 6 and 34 is run if the mailbox was opened.
Line 7 - Gets the number of mail messages in the mailbox.
Line 8,9, 27 - This line cycles through each item in the mailbox. Lines 8 and 27 define the code to be run for each mailbox item. Code between brackets 9 and 27 is run.
Line 10 - Gets the header of the mail message into a string.
Line 11 - Displays the value of the message header.
Line 12 - Converts the string to lower case characters.
Line 13,14,26 - Tests the converted header string to see if it contained the string "update?". It returns the string starting at where the string "update?" starts to the end of the searched string. If the string "update?" is found, code between lines 14 and 26 is run.
Line 15 - Splits the string into words that were seperated by spaces, in this case getting the string up to the end of the string.
Line 16, 17, 25 - Returns the string from the "?" until the end. If the returned valus is not NULL, code between lines 17 and 25 is run.
Line 18 - Strips the "?", and returns the ID as a string.
Line 19 - Converts the ID string to an integer.
Lines 20-24 - Update values in an SQL database to reflect the fact that there is an update request for a specific ID number.
Line 28 - Deletes messages that have been read.
Line 29 - Closes the mailbox.

No comments:

Post a Comment

Popular Posts