How to get a file name from a form to send it as an attachment via e-mail?

5 views
Skip to first unread message

Nuno Grenhas

unread,
Jun 8, 2007, 1:19:48 PM6/8/07
to mimemessage-dev, mle...@acm.org
Hello,

I'm using MIME message with SMTP e-mails sending class and I want to
send emails with attachments:

I tested with "test_attachment_message.php" and it works.
But in my script I have to get a file located in the disk with this
input:
<input name="attachment" type="file" id="attachment" />

1 - How can I get the FileName using a form with that input?

$image_attachment=array(
"FileName"=>"http://www.phpclasses.org/graphics/logo.gif", // Here
I have to put the file name variable, Right?
"Content-Type"=>"automatic/name",
"Disposition"=>"attachment"
);
$email_message->AddFilePart($image_attachment);

2 - Can I send an attached file located in my local disk or is it
necessary to upload it first to a site folder?
3 - How can I send mutliple attachments in the same message?

Pardon my lack of experience but I'm stuck in this problem.

Regards,
Nuno

Nuno Grenhas

unread,
Jun 11, 2007, 10:41:33 AM6/11/07
to mimemessage-dev
I think I solved the problem. I browse and select a file trough the
form with a file type field named file, then copy the file to a site
folder named "files" and then added it to the e-mail message with
AddFilePart. I wonder if it works in a remote server.

if (isset($_FILES['file']['tmp_name']) && $inserir == "1") {

$file = $_FILES['file']['name'];
if (copy($_FILES['file']['tmp_name'],"files/".$_FILES['file']
['name'])) {

$file_attachment = array(
"FileName"=>"files/".$file,


"Content-Type"=>"automatic/name",
"Disposition"=>"attachment"
);

$email_message->AddFilePart($file_attachment);
}

However I'm not sure it's the right way. Anyone can tell me if it's
right?

Regards,
Nuno

Manuel Lemos

unread,
Jun 11, 2007, 11:50:16 AM6/11/07
to mimemes...@googlegroups.com
Hello,

on 06/11/2007 11:41 AM Nuno Grenhas said the following:

That will work. You do not need to copy the file to a different path
unless you want to. Anyway, for security precautions I would recommend
that you use something like "files/".basename($_FILES['file') to avoid
problems when somebody tries to spoof a harmful file name.

--

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

Nuno Grenhas

unread,
Jun 12, 2007, 7:28:52 AM6/12/07
to mimemessage-dev
OK! It's working fine!
Obrigado Manuel, abraço.

Nuno Grenhas

unread,
Jun 13, 2007, 2:54:55 PM6/13/07
to mimemessage-dev, mle...@acm.org
Now I'm having another problem:

While uploading a file with more than 1MB it sends the file, attached
to the message, but returns the error:
"it was not possible to read line from the SMTP server: data access
time out"
wich is from the "smtp_class", in function GetLine, located in the
file smtp.php.
With small files no problem.

Below is the code where I use smtp_message_class:

-----------------------------------------------------------------------------

$email_message=new smtp_message_class;

$email_message->SetEncodedEmailHeader("To",$to,$to_name);
$email_message->SetEncodedEmailHeader("From",$from,$from_name);
$email_message->SetEncodedEmailHeader("Reply-To",$geral,$geral);
$email_message->SetHeader("Return-Path",$from);
$email_message->SetEncodedEmailHeader("Errors-To",$from,$from_name);
$email_message->SetEncodedHeader("Subject",$msg_assunto);
$email_message->AddQuotedPrintableHTMLPart($email_message-
>WrapText($msg));

// If a file was selected to attach
if (isset($_FILES['anexo']['tmp_name']) && $inserir == "1") {

$err_file_size = "";

// Testing file size
if ( $_FILES['anexo']['size'] > 2000000 ) {
$err_file_size = "Error: File is bigger than 2MB.";
}

$anexo = basename($_FILES['anexo']['name']);

// don't know why but it only works if I copy the file to a site
folder
if (copy($_FILES['anexo']['tmp_name'],"../anexos/".$anexo)) {

$file_attachment = array(
"FileName"=>"../anexos/".$anexo,


"Content-Type"=>"automatic/name",
"Disposition"=>"attachment"
);

$email_message->AddFilePart($file_attachment);
}
}

// Validating file size
if ( !empty( $err_file_size ) ) {
$aviso = "Error with attachment:<br />" . $err_file_size;

} else {
$MsgResult=$email_message->Send();

for($recipient=0,Reset($email_message->invalid_recipients);
$recipient<count($email_message-
>invalid_recipients);Next($email_message->invalid_recipients),
$recipient++)
$aviso = "Invalid e-mail adress: ".Key($email_message-
>invalid_recipients)." Erro: ".$email_message-
>invalid_recipients[Key($email_message->invalid_recipients)]."\n";

if ( $MsgResult == "" ) {
$aviso = "<font color=\"#009900\">Message sent<br />";

// Delete the file
if (file_exists("../anexos/".$anexo)) {
unlink("../anexos/".$anexo);
}

} else {
$aviso = "ERROR: Message not sent!<br />$MsgResult<br />";
}
}

Nuno Grenhas

unread,
Jun 14, 2007, 8:15:12 AM6/14/07
to mimemessage-dev
Summarizing the problem with file attachments:

- Small files are uploaded and sent with the message with no errors;
- Files with more than 1MB are sent but shows the error: "it was not


possible to read line from the SMTP server: data access time

out" (from the smtp_class wich is integrated with smtp_message_class);
- Files with more than 2 MB are not uploaded but the message is sent
without errors.

php.ini configuration:
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

I'm still stuck on this. My smtp server allows much more than 2MB
attachments, so I don't know where is the problem.

Nuno Grenhas

unread,
Jun 14, 2007, 12:58:32 PM6/14/07
to mimemessage-dev
Reply all
Reply to author
Forward
0 new messages