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>

      <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(
"sender@domain.com" => "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

+3


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'])) {


$message->attach(

Swift_Attachment::fromPath($_FILES['fieldAttachment']
['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])

);


}

      

0


source


Replace your code

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

      



On this

$message->attach(
Swift_Attachment::fromPath($_FILES['fieldAttachment']['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
);

      

+1


source


Check form loading. You may have a bug.

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

      

0


source







All Articles