IanTh
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_fil e, $type_of_uploaded_file, $size_of_uploaded_file) = GetUploadedFileInfo();
if(!Validate($name_of_uplo aded_file, $type_of_uploaded_file, $size_of_uploaded_file, $max_allowed_file_size)) {
exit();
}
LoadUploadedFile($name_of_ uploaded_f ile);
$path_of_uploaded_file = "uploaded/" . $name_of_uploaded_file;
include_once(/PEAR/Mail.ph p);
include_once(/PEAR/Mail/mi me.php);
ComposeMail($path_of_uploa ded_file);
//////////////////// Functions ////////////////////////
function GetUploadedFileInfo() {
$file_info[] = basename($_FILES['uploaded _file']['n ame']);
$file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
$file_info[] = $_FILES["uploaded_file"][" size"]/102 4;
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_allow ed_file_si ze ) {
echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_for m.html'>Cl ick Here to upload a smaller sized file.</a>";
return false;
}
$allowed_extension = array("jpeg", "gif", "bmp", "doc" );
for($i=0; $i<sizeof($allowed_extensi on); $i++) {
$allowed_extension[$i] = strtoupper($allowed_extens ion[$i]);
}
$type_of_uploaded_file = strtoupper($type_of_upload ed_file);
if(!(in_array(strtoupper($ type_of_up loaded_fil e),$allowe d_extensio n))) {
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_for m.html'>Cl ick Here to upload a file with allowed extension.</a>";
return false;
}
return true;
}
function LoadUploadedFile($name_of_ uploaded_f ile) {
move_uploaded_file($_FILES ["uploaded _file"]["t mp_name"], "uploaded/" . $name_of_uploaded_file);
return true;
}
function ComposeMail($name_of_uploa ded_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}$lin e";
$text .= $conc;
$space = ' ';
}
$message = new Mail_mime();
$message->setTXTBody($text );
$message->addAttachment($n ame_of_upl oaded_file );
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject);
$headers = $message->headers($extrahe aders);
$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
<?php
$max_allowed_file_size = "100"; // this is size in KB
list($name_of_uploaded_fil
if(!Validate($name_of_uplo
exit();
}
LoadUploadedFile($name_of_
$path_of_uploaded_file = "uploaded/" . $name_of_uploaded_file;
include_once(/PEAR/Mail.ph
include_once(/PEAR/Mail/mi
ComposeMail($path_of_uploa
//////////////////// Functions ////////////////////////
function GetUploadedFileInfo() {
$file_info[] = basename($_FILES['uploaded
$file_info[] = substr($file_info[0], strrpos($file_info[0], '.') + 1);
$file_info[] = $_FILES["uploaded_file"]["
return $file_info;
}
function Validate($name_of_uploaded
if($size_of_uploaded_file>
echo "Size of file is greater than" . $max_allowed_file_size . " KB. <a href='attachment_email_for
return false;
}
$allowed_extension = array("jpeg", "gif", "bmp", "doc" );
for($i=0; $i<sizeof($allowed_extensi
$allowed_extension[$i] = strtoupper($allowed_extens
}
$type_of_uploaded_file = strtoupper($type_of_upload
if(!(in_array(strtoupper($
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_for
return false;
}
return true;
}
function LoadUploadedFile($name_of_
move_uploaded_file($_FILES
return true;
}
function ComposeMail($name_of_uploa
$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';
// --------------------------
$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}$lin
$text .= $conc;
$space = ' ';
}
$message = new Mail_mime();
$message->setTXTBody($text
$message->addAttachment($n
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject);
$headers = $message->headers($extrahe
$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
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.no1 net.co.uk/ user/htdoc s/send-ema il-form.ph p on line 10
line 10: include_once(/PEAR/Mail.ph p);
does that mean pear insn't installed on my server
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.no1
line 10: include_once(/PEAR/Mail.ph
does that mean pear insn't installed on my server
Try
include_once('/PEAR/Mail.p hp');
include_once('/PEAR/Mail.p
ASKER
why?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.phpclasses.org/browse/package/3564.html
and develop it to suite your code