In this article I am demonstrating how to send an E Mail with attachments in PHP. I have developed a class called Mailer.php.It exposes a static method called SendMail that actually sends the E Mail and true/false for success or failure.
Here is the complete class.
Class.Mailer.php
<?php
class Mailer
{
public static function SendMail($mailto,$mailfrom,$mailsubject,$_FILES,$msg)
{
$hasattachment=false;
$msg=stripslashes($msg);
if(!$_FILES)//Check if a FILES collection exists
{
//If no file exists, just mail it
$mailfrom="From: $mailfrom";
if(mail($mailto,$mailsubject,$msg,$mailfrom))
return true;
else
return false;
}
else
{
//Try and attach the file
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
if (is_uploaded_file($attachment))
{
//attaching the file
$hasattachment=true;
$fp = fopen($attachment, "rb"); //Open the attachment
$data = fread($fp, filesize($attachment)); //Read the attachment
$data = chunk_split(base64_encode($data)); //Split it and encode it for sending
fclose($fp);
}
}
$headers = "From: $mailfrom\n";
$headers .= "Reply-To: $mailto\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $mailfrom>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . "$mailfrom" . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= "$msg\n";//Add the message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The attachment
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
if(mail($mailto,$mailsubject,$message,$headers))
return true;
else
return false;
}
}
?>
The HTML Page that contains the HTML Form.
Mail.html
<body bgcolor="orange">
<center>
<form enctype="multipart/form-data" id="name" name="name" action="SendMail.php" method="post">
<table width="80%" border="2">
<tr><td colspan="2" align="center"><h1>Email System through PHP</h1></td></tr>
<tr><td width="15%">Sender</td><td><input type="text" id="sender" name="sender"></td></tr>
<tr><td width="15%">Receiver</td><td><input type="text" id="receiver" name="receiver"></td></tr>
<tr><td width="15%">Subject</td><td><input type="text" id="subject" name="subject"></td></tr>
<tr><td width="15%">Message</td><td><textarea id="message" name="message" rows="7" cols="30"></textarea></td></tr>
<tr><td>Attachment</td><td><input type="file" name="attachment" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Submit"></td></tr>
</form>
</table>
</center>
The SendMail.php page
SendMail.php
<?php
require_once('Class.Mailer.php');
//if no attachments are desired use null instead of $_FILES
$result=Mailer::SendMail($_POST['receiver'],$_POST['sender'],$_POST['subject'],$_FILES,$_POST['message']);
if($result)
print("Mail Sent");
else
print("Mail Sending Failed");
?>
The received Mail
And here is the attachment.
In further posts, we will learn to do the same things in Java Server Programming & ASP.NET.
No comments:
Post a Comment