Upload temp file creation problems

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
PattyB
Forum Newbie
Posts: 2
Joined: Thu Oct 26, 2006 1:31 pm

Post by PattyB »

I have tried uploading the files using the below mentioned technique however when I open the uploaded file this is all it contains:

C:\WINDOWS\TEMP\php6E.tmp

Where the .tmp file will change values.

Do you know what could be causing this?

d11wtq wrote:
jayshields wrote:I've never sent an email with an attachment via PHP. It's becoming confusing for me. I decided to use Swift, but I can't find any attachment examples where it sends an email with a user uploaded file; they are always server side files.

Does that mean I have to upload the file, add it as attachment, send the email, then delete the uploaded file? I'm not 100% sure how attachments work anyways - would I have to wait until the user has recieved the email and grabbed the attachment before deleting the uploaded file?

Thanks.
Upload the file, using $_FILES['form_field_name']['tmp_name'] as the path:

Code: Select all

$swift->addAttachment($_FILES['foo']['tmp_name'], $_FILES['foo']['name'], $_FILES['foo']['type']);
PHP should remove temporary files itself if I remember correctly... you'd need to use move_uploaded_file() if you wanted to keep a copy on the server.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This was split from the SwiftMailer Code Snippet thread.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You're passing the path to the file, when you should be passing the file's contents. Use file_get_contents()...

Code: Select all

$swift->addAttachment(
    file_get_contents($_FILES['foo']['tmp_name']), $_FILES['foo']['name'], $_FILES['foo']['type']);
PattyB
Forum Newbie
Posts: 2
Joined: Thu Oct 26, 2006 1:31 pm

Post by PattyB »

d11wtq wrote:You're passing the path to the file, when you should be passing the file's contents. Use file_get_contents()...

Code: Select all

$swift->addAttachment(
    file_get_contents($_FILES['foo']['tmp_name']), $_FILES['foo']['name'], $_FILES['foo']['type']);
Yes, that was it. Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Glad you got it sorted. I have to apologise since re-reading your post I see it was me who first provided such code. My bad :oops:
Post Reply