Solved

sending mail with attachment using pear mail

Posted on 2009-07-13
5
710 Views
Last Modified: 2012-05-07
my script that send contents of a form and an attachment is for use as a generic form processing script

<?php
$max_allowed_file_size = "100"; // this is size in KB
list($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file) = GetUploadedFileInfo();
if(!Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size)) {
   exit();
}
LoadUploadedFile($name_of_uploaded_file);
$path_of_uploaded_file = "uploaded/" . $name_of_uploaded_file;

   include_once(/PEAR/Mail.php);
   include_once(/PEAR/Mail/mime.php);

ComposeMail($path_of_uploaded_file);


//////////////////// Functions ////////////////////////

function GetUploadedFileInfo() {
   $file_info[] = basename($_FILES['uploaded_file']['name']);
   $file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
   $file_info[] = $_FILES["uploaded_file"]["size"]/1024;
   return $file_info;
}


function Validate($name_of_uploaded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size) {
   if($size_of_uploaded_file>$max_allowed_file_size ) {
       echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_form.html'>Click Here to upload a smaller sized file.</a>";
      return false;
   }
$allowed_extension = array("jpeg", "gif", "bmp", "doc" );
for($i=0; $i<sizeof($allowed_extension); $i++) {
   $allowed_extension[$i] = strtoupper($allowed_extension[$i]);
}
$type_of_uploaded_file = strtoupper($type_of_uploaded_file);
if(!(in_array(strtoupper($type_of_uploaded_file),$allowed_extension))) {
   echo "You have uploaded a file with an extension of " . $type_of_uploaded_file . " . This type is not allowed. Please upload a file with allowed image extensions like jpg, jpeg, bmp, gif, doc. <a href='attachment_email_form.html'>Click Here to upload a file with allowed extension.</a>";
   return false;
}
   return true;
}


function LoadUploadedFile($name_of_uploaded_file) {
   move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "uploaded/" . $name_of_uploaded_file);
   return true;
}


function ComposeMail($name_of_uploaded_file) {
   $name = $_POST['name'];
   $user_message = $_POST['message'];
   $to = "ian.thompson@gbpost.com";
   $subject="An email with attachement is sent";
   $from = "SenderEmail@hotmail.com";
   
   // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty. Click the back button to return to the form";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;

for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
   
   $message = new Mail_mime();
   $message->setTXTBody($text);
   $message->addAttachment($name_of_uploaded_file);
   $body = $message->get();
   $extraheaders = array("From"=>$from, "Subject"=>$subject);
   $headers = $message->headers($extraheaders);
   $mail = Mail::factory("mail");
   $mail->send($to, $headers, $body);
   echo "Your Email with attachment was sent.";
}
?>

doesn't add the file as attachment I just get the name of the file as its in a form element
the loop for getting the form elements work and I get the mail but I do not get the file uploaded to the uploaded folder as I was checking the folder using filezilla during testing this script

HELP
0
Comment
Question by:IanTh
  • 3
5 Comments
 
LVL 7

Expert Comment

by:ajkhalifa
Comment Utility
check a ready made simple classes like:
http://www.phpclasses.org/browse/package/3564.html
and develop it to suite your code
0
 
LVL 30

Author Comment

by:IanTh
Comment Utility
why it does work as I had it on my test web server at home linux, apache , php5 & pear and it was working
its just not working on my online test web server

now when I run it on ian.no1net.co.uk
I get Parse error: syntax error, unexpected '/' in /home/fhlinux198/i/ian.no1net.co.uk/user/htdocs/send-email-form.php on line 10

line 10: include_once(/PEAR/Mail.php);

does that mean pear insn't installed on my server
0
 
LVL 6

Expert Comment

by:basic612
Comment Utility
Try

include_once('/PEAR/Mail.php');
0
 
LVL 30

Author Comment

by:IanTh
Comment Utility
why?
0
 
LVL 30

Accepted Solution

by:
IanTh earned 0 total points
Comment Utility
fixed it myself by not using pear

if anyone needs a script to do the same her's my new working php code

<?php
$fileatt = ''; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "test"; // Filename that will be used for the file as the attachment
$fromadd = 'ian@ian.no1net.co.uk'; // used for the fifth element in fasthost
$email_from = 'ian@gbpost.com'; // Who the email is from
$email_subject = "testing"; // The Subject of the email
$email_txt = "testing with attachment"; // Message that the email has in it

$email_to = $_POST['email address']; // Who the email is too

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= $email_txt;
// --------------------------Do not edit below this line--------------------------
$text = "<br>\nResults from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty. Click the back button to return to the form";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;

for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line<br>";
$text .= $conc;
$space = ' ';
}

$email_message .= $text;
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";


$ok = @mail($email_to, $email_subject, $email_message, $headers,"-f".$fromadd);

if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>


works fab now
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now