Link to home
Start Free TrialLog in
Avatar of IanTh
IanThFlag for United Kingdom of Great Britain and Northern Ireland

asked on

sending mail with attachment using pear mail

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
Avatar of ajkhalifa
ajkhalifa
Flag of Saudi Arabia image

check a ready made simple classes like:
http://www.phpclasses.org/browse/package/3564.html
and develop it to suite your code
Avatar of IanTh

ASKER

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
Try

include_once('/PEAR/Mail.php');
Avatar of IanTh

ASKER

why?
ASKER CERTIFIED SOLUTION
Avatar of IanTh
IanTh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial