Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email attachments that are uploaded from web form with SwiftMailer

I am trying to use swftmailer to - upload a file (jpg,ppt,pdf,word etc with a max upload of eg: 6MB) from a form on a html web page, - email the content of the form with the attached file via swiftmailer.

My form is created & I am using $_POST to capture the field values in a thankyou.php page. The email is sending, but I cant get the file to attach!

The upload/attach file needs to be an option, I found a resource online that only sends the info if a file is uploaded, which is not always going to be the case, so I have turned to swiftmailer which looks great, but in the documentation/examples it specifies a path to upload the attachment too, I just want to be able to read in the file (not specify a path) and send it as an attachment to the given email address along with the other form fields, Name Email,Phone & Message.

Any help would be greatly appreciated.

My HTML (contact.html)

 <div id="form-main">
  <div id="form-div">
    <form class="form" method="post" action="thank-you.php" enctype="multipart/form-data">

      <p class="name">
        <input name="fieldFormName" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
      </p>

      <p class="email">
        <input name="fieldFormEmail" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
      </p>

       <p class="phone">
        <input name="fieldFormPhone" type="number" class="validate[required,custom[onlyLetter],length[0,20]] feedback-input" placeholder="Phone" id="phone" />
      </p>

      <p class="text">
        <textarea name="fieldDescription" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
      </p>

      <p class="upload">
      <small>Send us a Drawing or file</small>
        <input name="fieldAttachment" id="attachment" class="feedback-input" type="file">
      </p>


      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>
    </form>
  </div>
  </div>

My PHP Code:

<?php
require_once 'lib/swift_required.php';

$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName'];
$fromPhone = $_POST['fieldFormPhone'];
$fromMessage = $_POST['fieldDescription'];
$fromAttachment = $_POST['fieldAttachment'];

// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"[email protected]" => "Sender Name"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody(
"From: " . $fromName . "\n" .
"Email: " . $fromEmail . "\n" .
"Phone: " . $fromPhone . "\n" .
"Message: " . $fromMessage . "\n"
);
$message->setFrom("$fromEmail", "$fromName");

// *** This is the part I am struggling to understand *** //
$message->attach(Swift_Attachment::newInstance('$fromAttachment'));

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

?>

Thank you in advance

like image 694
unrealindeed Avatar asked Mar 19 '23 08:03

unrealindeed


1 Answers

Replace your code

$message->attach(Swift_Attachment::newInstance('$fromAttachment'));

on this

$message->attach(
Swift_Attachment::fromPath($_FILES['fieldAttachment']['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
);
like image 167
d-velop Avatar answered Apr 25 '23 04:04

d-velop