How to send a mail with PHP ASP or Java
Posted on 7th April 2006 by Keith DsouzaMany of us want to have a personalized email send program that will allow us to send emails, in this tutorial i am going to write about how this can easily done with the most used web based programming language.
Starting with PHP
PHP has a inbuilt function mail() that allows us to send an email.
You can make use of the following method to send a email with php.
##################################################
# Function to send a mail to the user
# @param toemail - the to email
# @param ccmail - the cc email
# @param bccmail - the bcc email
# @param fromemail - the from email
# @param subject - the subject for the message
# @param emailbody - the actual body
# Return Type: boolean
###################################################
function sendmail($toemail, $ccemail, $bccemail, $fromemail, $fromname, $subject, $message_body){
$headers = "";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromname <$fromemail>\r\n";
if(strlen(trim($ccemail)) > 0 && $ccemail != null){
$headers .= "Cc: $ccemail\r\n";
}
if(strlen(trim($bccemail)) > 0 && $bccemail != null){
$headers .= "Bcc: $bccemail\r\n";
}
/* and now mail it */
$mail = mail($toemail, $subject, $message_body, $headers);
//print ( $mail )? "mail sent" : "mail failed";
return $mail;
} //end sendmail
Here is the explanation for the above code
$headers = "MIME-Version: 1.0\r\n";
The MIME-Version: header tells the type of email we are sending I am setting it to 1.0
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
The Content-type: header tells that this email is of type text/html
$headers .= "From: $fromname <$fromemail>\r\n";
The from header is the header which tells by whom this mail has been sent. In this header you can also specify a personalized name for the email address. For eg. Keith Dsouza <keith @ keithdsouza.com> (note do not use the space in between the email id and the @ i have kept so as to keep away email spamming bots.)
$headers .= "Cc: $ccemail\r\n";
The Cc: header sets the recipients who should recieve this mail as a carbon copy, note you can specify email ids seperated by either ',' or ';'.
$headers .= "Bcc: $bccemail\r\n";
The Bcc: header sets the recipients who should recieve this mail as a blind carbon copy, note you can specify email ids seperated by either ',' or ';'.
$mail = mail($toemail, $subject, $message_body, $headers);
mail($toemail, $subject, $message_body, $headers);
Send the mail by setting the proper parameters, first parameter is the toemail, second is the subject for the message and third is the body for the message, the final parameter is the headers we have set earlier.
That's it now you are all setup to send your first email with php, please let me know if you have any doubts or problems.
Technorati Tags: php mail, send email, email using php
