1

I have a contact form on my website that allows the user to send files as attachments to an email. Now my reasoning is that, I do not need or necessarily want every image to be uploaded to the website server, but when using the form input file I gets uploaded anyways.

It makes sense that the form uploads the file as this is its function but I would like to know if this is inevitable or if there is a way to still send the images as attachments but not have it uploaded to the server.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Simon Erasmus
  • 134
  • 12
  • There sure is a way. Easiest method is with phpmailer https://github.com/PHPMailer/PHPMailer - Yet seeing your other question/answer http://stackoverflow.com/q/37444737/, you're using swiftmailer. So your question's unclear right now. – Funk Forty Niner May 28 '16 at 17:30
  • I am using swiftmailer, is there any way to achieve the same goal using this? @Fred-ii- – Simon Erasmus May 28 '16 at 17:32
  • Sure, just don't use code that has uploading features; just attach them. It doesn't need to be saved to the server. – Funk Forty Niner May 28 '16 at 17:32
  • 1
    If the server is emailing the file then the file needs to be on the server in some form or fashion. – AbraCadaver May 28 '16 at 17:40
  • Thats what I thought, but is it common practice to keep it on the server or should it be deleted from the server after being sent to the recipient? @AbraCadaver – Simon Erasmus May 28 '16 at 17:41
  • 1
    If you don't need it then delete it. It's sent to a temp dir so that could be flushed periodically. – AbraCadaver May 28 '16 at 17:43
  • Here, not needed to upload to a folder http://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php but that's with phpmailer. I'm sure you can figure it out to work with swiftmailer, or just switch to it ;-) – Funk Forty Niner May 28 '16 at 17:43
  • @AbraCadaver *"If the server is emailing the file then the file needs to be on the server in some form or fashion."* - Not always. ;-) – Funk Forty Niner May 28 '16 at 17:45
  • @Fred-ii- Then post as an answer. I can't think of a non-elaborate way. The normal file upload puts it in a temp dir. – AbraCadaver May 28 '16 at 17:51
  • @AbraCadaver OP already posted his own answer ;-) so I'm out of this loop *lol* – Funk Forty Niner May 28 '16 at 17:52
  • @AbraCadaver *"The normal file upload puts it in a temp dir."* - Agreed ;-) and automatically deleted once sent/processed successfully, but you knew that ;-) – Funk Forty Niner May 28 '16 at 17:56

2 Answers2

1

Pulled from this answer https://stackoverflow.com/a/23849972/ (and having spent some time to find them a solution).

Sidenote: I upvoted the answer it was pulled from, just so you know.

"I assigned $_FILES['attachment']['tmp_name'] to a temporary variable and it worked! Dont know why but that solved it for me. Here's my code"

// Swiftmail commands ====================================
require_once('./swiftmailer/lib/swift_required.php');

$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 587)
->setUsername('email@host.com')
->setPassword('pass');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance()
->setSubject($subject_temp)

->setFrom(array($from_email => $full_name))

->setTo(array('email@host.com' => 'Jack'))

->setBody($message_temp)

->attach(Swift_Attachment::fromPath($file_temp_name)

->setFilename($name_of_file));

$result = $mailer->send($message);

// Swiftmail commands ====================================

Where $file_temp_name = $_FILES['attachment']['tmp_name']; and $name_of_file = basename($_FILES['attachment']['name']);

  • So in turn (and was something I knew could be done), you would be using the temporarily stored file on the server and attaching it to the mail, which automatically gets deleted once it has been successfully served/processed.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

As stated in the comments:

1.) Even though it is possible to have the files automatically removed after posting, the email function I am using is not set up that way. Thus an upload to the server is inevitable. Or at least to my knowledge.

2.) I can delete the file from the server after sending the mail.

Will update if a solution is discovered that solves my problem the best.

Simon Erasmus
  • 134
  • 12
  • Not necessarily and beg to differ. – Funk Forty Niner May 28 '16 at 17:53
  • Care to elaborate @Fred-ii- – Simon Erasmus May 28 '16 at 17:53
  • I've already commented up there. It's pretty much self-explanatory ;--) – Funk Forty Niner May 28 '16 at 17:53
  • 1
    Plus, I've attached files before using PHP's `mail()` function without having to store them on the server. The (uploaded temp file) automatically gets deleted once it's been sent/processed. TBH, I've never used Swiftmailer. – Funk Forty Niner May 28 '16 at 17:55
  • Ok, my apologies for disregarding your answer. I am not using the mail() function so guess it will not work for my scenario but is possible it seems. Thanks @Fred-ii- – Simon Erasmus May 28 '16 at 17:58
  • No worries Simon. I'm sure there's a way to do it with Swiftmailer, at least one would figure it would, given that it can be done with `mail()` and phpmailer. If it can't be done with Swiftmailer, then there's something terribly wrong with their method *lol* - Just saying ;-) and you're welcome. *Cheers* – Funk Forty Niner May 28 '16 at 18:00
  • 1
    Here's your solution http://stackoverflow.com/a/23849972/ and using the temp file; I knew it was possible. Would you like me to pull that code from it and make it an answer? For having found it ;-) – Funk Forty Niner May 28 '16 at 18:15