Solved

sending mail with attachment using pear mail

Posted on 2009-07-13
5
727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Expert Comment

by:ajkhalifa
ID: 24842103
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
ID: 24842642
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
ID: 24843924
Try

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

Author Comment

by:IanTh
ID: 24847208
why?
0
 
LVL 30

Accepted Solution

by:
IanTh earned 0 total points
ID: 24934740
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

740 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