PHP — Sending e-mail / SMS using forms
Hello Readers,
In this article, I’ll talk about how easy it is to add an e-mail or SMS functionality to your webpage. In the first part, we’ll cover how to send an sms using forms,then we’ll go cover sending e-mails.
Sending SMS through webpage
Pre-requisite — An account with sms providing company. I used bhashsms.com. They provide APIs and also various options in the package which makes our task easier.
Once your account has been setup and active, you can test with following piece of code which is can be called on form action. You can also use this anywhere in you webpage to send SMS notifications —
$user= “9xxxxxxxxx”;
$password= “***”;
$sender_id= “xxxx”;
$receiver = “ABC”;
$text= “Dear”.$receiver.“,This is a test text message”;
$priority= “ndnd”;
$sms_type= “normal”;
$data = array(‘user’=>$user, ‘pass’=>$password, ‘sender’=>$sender_id, ‘phone’=>$phone, ‘text’=>$text, ‘priority’=>$priority, ‘stype’=>$sms_type);
// Send the POST request with cURL
$ch = curl_init(“http://bhashsms.com/api/sendmsg.php?”);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
Remember, don’t be too aggressive on notification part. People only like to be notified when there is something exciting to come :)
Sending Email through webpage
This part is easy as there is no additional 3rd party involved, all you need is a valid e-mail ID. Here is a piece of code, you can just give it a try —
$toEmail = “YourEmailId@xyzz.com”;
$fromEmail = “myEmailId@xyzz.com”;
$subject = “Your house is on fire.”;
$message = “Oh don’t bother, you don’t own a house”;
$header = “From:”.$fromEmail.”\r\n”;
$header .= “MIME-Version: 1.0\r\n”;
$header .= “Content-type: text/html\r\n”;
$retval = mail ($toEmail, $subject, $message, $header);
//Check if mail was sent
if( $retval == true )
{ echo “Mail Sent Successfully”; }
else{ echo “Mail was not Sent Successfully”; }
Again, use this wisely.
Hope, this works for you, it worked for me at least! Just give it a try.
Please feel free to comment and share your thoughts. Feedback regarding the content will also be helpful for my future posts. Thanks!