Saturday, June 23, 2007

PHP Mailer with Attachments

Most recently I was able to write some php codes to send a mail with an attachment. The task is to get some input from the user using an html page and send a mail through the web page. This involved in file attachment as well, where it was equipped with 5 attachments but limited to 10mb. (I used the PEAR package to send the mail)

I wrote a separate page to input the mail configurations (mail_config.php) and a separate page to handle the mailing process (sendmail.php). Well, this is how it goes,

//mail_config.php
<?
// Mail sent to
$to="raji@email.com";
//The SMTP host to connect to.
$host = "mail.rajiserver.com";
//The port the SMTP server is on.
$port = 25;
//Mail account username
$username = "raji@rajimail.com";
//Mail account password
$password = "rajipassword";
//Mail from
$from = "raji@phpmail.com";?>

//sendmail.php
<?php

// --- PHP MULTIPLE MAIL ATTACHMENTs V 1.0 ---
// --- Visit Authors website at http://hotstepz.blogspot.com
// --- This Script needs PEAR package to send mails
// --- Written on : 12 June 2007
// --- All Rights Reserved ------------------------

require_once "Mail.php";
include "mail_config.php";

//This variable is to check the total size of the files added
//This works when the attaching code reads the file to attach
$tot_file_size = 0;

//Variables to send through email body, If you have something to read from
//read from a html page
//Examples
//Date and time of the mail sent
$datetime = date("F j, Y, g:i a");
$emailfrom = $_POST["email"];
$text1 = $_POST["text1"];
$text2 = $_POST["sid"];
$text3 = $_POST["service_status"];
$text4 = $_POST["text4"];
$text5 = $_POST["text5"];

//-- ---------------- HTML Table begins ---------------------

// Message HTML Format, values will be organized into a table
//was taken originally from the final product...
$message= '
<table cellspacing="0" cellpadding="8" border="0" width="400">
<tr>
<td colspan="2"><b>Service Request</b></td>
</tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>Date and Time</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$datetime.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>Email From</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$emailfrom.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>text1</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$text1.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>text2</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$text2.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"> </td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"> </td>
</tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>text3</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$text3.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>text4</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$text4.'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"></td></tr>

<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">
<strong>text5</strong></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$request.'</td>
</tr>

<tr><td colspan="2" style="padding:0px;"></td></tr>
</table>
';
//-- ---------------- HTML Table Ends---------------------

//Normal headers

$headers = "From: $from>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";

// This two steps to help avoid spam

$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

// With message

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";

// Attachment headers

//Set attachment counter 0
$i=0;
for($i=0; $i < count($upfile); $i++)
{
if ($_FILES["upfile"]["name"][$i])
{
$filename=$_FILES["upfile"]["name"][$i];
$filetype=$_FILES["upfile"]["type"][$i];
$filesize=$_FILES["upfile"]["size"][$i];
//Converting the file size from bytes to megabytes
$att_file_size = round($filesize/(1024*1024));
$tot_file_size = $tot_file_size + $att_file_size ;
$filetemp=$_FILES["upfile"]["tmp_name"][$i];

//Check Uploaded File Type
//--- Specify manually the allowed file type ---
if(/*$filetype=="application/octet-stream" or */$filetype=="text/plain" or $filetype=="application/msword" or "application/pdf")
{
// MAIL HEADERS with attachments

$fp = fopen($upfile[$i], "rb");
$file = fread($fp, $upfile_size[$i]);
fclose($fp);
$file = chunk_split(base64_encode($file));
$num = md5(time());

$headers .= "--{$mime_boundary}\n" .
"Content-Type: {$upfile_type[$i]};\n" .
" name=\"{$upfile_name[$i]}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$upfile_name[$i]}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$file . "\n\n";
}
else
{
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Incompatible file format uploaded at </font>';
$filenum = $i + 1;
echo "at File $filenum. ";
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Mail was not sent. </font>';
}
}
}

// Check Uploaded File Size
if ($tot_file_size > 10)
{
echo "File uploads limit exceeded, Max 10MB total size allowed.
You uploaded $tot_file_size MB" ;
}
else
{
// MAIL SUBJECT
$subject = "This php email is from $emailfrom";

// SEND MAIL
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,$headers);

$smtp = Mail::factory('smtp',

array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $message);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else
{
echo("<p>Message successfully sent!</p>");
}
}

?>

I was able to send the mail successfully (using bluebottle mail service) and retrieved it in my gmail mailbox. Please leave your comments if there are any problems or errors in my code. Until next mail,

Cheers!

No comments: