Friday, February 17, 2012

Nice pics




























Wednesday, November 10, 2010

Spend one minute

Due to some technical difficulties I am unable to post on this blog. Visit my current blog at http://www.sree-opinion.blogspot.com

Monday, November 1, 2010

Email/SMS Generation in php
PHP has a built-in mail function called mail(). This will send out Simple Mail Transport
Protocol (SMTP) mail to the world. The mail function is quite simplistic and basic, so
it usually is not the best choice, on its own, for heavier email tasks. It’s tricky to send
email messages with attachments, for example.
The PHP library, called PHPMailer, is just what the doctor ordered to fill the gap. It is
object-based and you can add it easily into a script with either an include or, more
appropriately, a require co mmand. You can fi nd t he P HP Mail er li br ar y at t he f oll o wi ng
URL: http://phpmailer.worxware.com/index.php?pg=phpmailer.

        After you have made reference to the PHPMailer class with a require command, simply
instantiate the class and start using it. Consider the following simple example taken
from the PHPMailer installation guide:
require("class.phpmailer.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// specify main and backup server
$mail->Host = "smtp1.example.com;smtp2.example.com";
// turn on SMTP authentication
$mail->SMTPAuth = true;
// SMTP username
$mail->Username = "petermac";
// SMTP password
$mail->Password = "secret";
$mail->From = "from@example.com";
$mail->FromName = "Mailer";
// name is optional
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com");
$mail->AddReplyTo("info@example.com", "Information");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// add attachments
$mail->AddAttachment("/var/tmp/file.tar.gz");
// optional attachment file name
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message
body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for
non-HTML mail clients";

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
As you can see, it’s pretty straightforward to build and send an email message, even
with attachments. One thing you can also do here is to use the $mail->AddAddress
method within a while loop to send a stock email message to a list of recipients from
your database records. If you are going to be doing a lot of email generation with different
aspects to the messages, be sure to get to know the methods of this class very
well so that you can be efficient with your code. Remember, though, that nobody likes
spam!
Sending out an SMS or text message to a cell phone is just as simple as sending out a
regular email message. Using the same PHPMailer library, you just need to make a few
simple adjustments so that you are sending information to a phone rather than to an
email account. Naturally, you have to know the phone number of the recipient and it
is best to have the permission of the recipient to send her a text message. Maintaining
this information in a database is, again, very convenient; just be sure you have that
permission.
Next, you need the address of the recipient’s SMS provider, the SMS domain. In Canada,
for example, there is Bell Aliant, Rogers, and Telus, and their respective SMS addresses
are: @txt.bell.ca, @pcs.rogers.com, and @msg.telus.com. Each provider should have
this domain address readily available on its website.
Here is some sample code that shows a text message being generated and sent:
if($OK_to_SMS) {
$SMSPhoneNum = str_replace('-', '',$CellNumber);
$sql = <<<SQL
SELECT SMSDomain
FROM SMSProvider
WHERE SMSProviderID = '$SMSProviderID'
SQL;
$db = new mysqli(
"localhost", // database server
$db_username, // username
$db_password, // password
$db_name ) // database name
or die("Cannot connect to server. Error code: %s\n" .
mysqli_connect_errno());
$result = $db->query($sql) or die("Could not execute SQL: $sql");
$row = $result->fetch_assoc();
$body = "Dear $Fname $Lname: \r\n \r\n"
. "We would like to inform you that you have been selected for jury duty. "
. "Check your email at $ContactEmail for more detailed information.";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "localhost";
$mail->From = "notification@juryduty.com";
$mail->FromName = "Law Courts of Anytown";
$mail->AddAddress($SMSPhoneNum . $row['SMSDomain']);
$mail->Body = $body;
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo "Jury duty notification not sent: SMS <br/>";
echo "Mailer Error: " . $mail->ErrorInfo . "<br/>";
} else {
echo "Jury duty notification sent";
}
}
Notice here that we first verify that it is OK to SMS message the recipient. We then do
a string replacement on the phone number to take out any dashes. You should also do
this for brackets around the area code, if they exist. This is so that the SMS phone
number will be in the correct format: just numbers, no punctuation. Then we prepare
the text message in a very similar fashion as before and send it as email to the concatenated
phone number and SMS provider domain.