Sending email via WAP with PHP

Once you have a WAP website up and running an interesting addition to the website would be allowing your users to send email via the website. Using PHP it is an easy matter to set up a WML form very similar to an HTML mailto form, a 'tell a friend about this website' or an 'email this article to a friend' type email script.

In this example we are going to set up a WML form to collect information from the user that the server will use to send an email. The tag allows the user to enter data, then the tag sends the data to the server. The tag within the tag contains the data gathered from the user via the tag.

WML code:

<wml>
<card id="email1" title="Email Example">
<p>
<do type="accept">
<go href="email2.php3" method="post">
<postfield name="SenderName" value="$(sendername)"/>
<postfield name="SenderEmail" value="$(senderemail)"/>
<postfield name="Email" value="$(email)"/>
<postfield name="Message" value="$(message)"/>
<postfield name="Subject" value="$(subject)"/>
</go>
</do>
Sender name:
<input title="Sendername" name="sendername"/> <br/>
Sender email:
<input title="Senderemail" name="senderemail"/> <br/>
Recipient email:
<input title="Email" name="email"/> <br/>
Subject:
<input title="Subject" name="subject"/> <br/>
Message:
<input title="Message" name="message"/> <br/>
</p>
</card>
</wml>

See it in action. ¦ Get complete code.

The PHP script that receives the data sends an email with the mail function. Mail() executes the UNIX sendmail command.

Usage:
mail(string recipient, string subject, string body, string additional_headers);
The additional_headers argument is used to set extra headers such as 'From', 'Reply-To', 'X-Mailer' etc.
Multiple extra headers are separated with a new line (\n). Mail that is successfully sent returns true or 1.

WML code:

<wml>
<card id="email2" title="Send Email">
<p>
<?php
$strSender
=$SenderEmail;
//In case the mail could not be delivered, send a message to the sender of the email.
$strHeader = "Return-Path: $strSender\nErrors-To: $strSender\nFrom:$SenderName<$strSender>";
if (
mail($Email,$Subject,$Message, "$strHeader")) :

print
"Message has been sent to $Email";

else :

print
"Could not send message to $Email";

endif;

?>

</p>
</card>
</wml>

See it in action. ¦ Get complete code.

There are also security concerns when implementing an email script on your website. The possiblity exists that someone will try to use it to send spam. You could restrict emails by HTTP_REFERER, making sure that the script submitting data, is a script that is on your website. Restrict the number emails that are sent to an email address within a certain number of minutes etc.

      Subscribe in a reader