Email attachments that are loaded from a web form using SwiftMailer

I am trying to use swftmailer to load a file (jpg, ppt, pdf, word, etc. with maximum load, for example: 6MB) from a form in a html webpage - write the form content using the attached file via swiftmailer.

My form is created and I am using $ _POST to grab the field values ​​in the thankyou.php page. The email is being sent, but I can't get the file to join.

The upload / attach file should be an option, I found a resource on the net that only sends information if the file is uploaded, which will not always be the case, so I turned to swiftmailer which looks great, but in the documentation / examples it specifies the path to upload an attachment, I just want to be able to read in the file (do not specify the path) and send it as an attachment to the specified email address along with other form fields, Name Email, Phone and 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 class="email">
        <input name="fieldFormEmail" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />

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

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

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

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


My PHP code:

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();
"" => "Sender Name"
$message->setSubject("This email is sent using Swift Mailer");
"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 *** //

// Send the email
$mailer = Swift_Mailer::newInstance($transport);



Thank you in advance


source to share

3 answers

I took DarveL's code and added an if statement to check if the file has been uploaded because I still want to send an email if the user does NOT attach the file. Below is the code, so now I just need to add a file type / size check.

if(is_uploaded_file($_FILES['fieldAttachment']['tmp_name'])) {








Replace your code



On this





Check form loading. You may have a bug.

<form enctype="multipart/form-data" action="" method="POST">




All Articles