0

I have a simple contact (resumé) form and I am trying to get the attachment to work with the message that is sent to me.

Here is the error:

Undefined index: fieldAttachment in D:\WWWRoot\castleventures.com\www\mail\lib\mail_sender.php on line 47 Fatal error: Uncaught Swift_IoException: The path cannot be empty in D:\WWWRoot\castleventures.com\www\mail\lib\classes\Swift\ByteStream\FileByteStream.php:48 Stack trace: #0 D:\WWWRoot\castleventures.com\www\mail\lib\classes\Swift\Attachment.php(67): Swift_ByteStream_FileByteStream->__construct(NULL) #1 D:\WWWRoot\castleventures.com\www\mail\lib\mail_sender.php(47): Swift_Attachment::fromPath(NULL) #2 D:\WWWRoot\castleventures.com\www\mail\mail.php(41): include('D:\WWWRoot\cast...') #3 {main} thrown in D:\WWWRoot\castleventures.com\www\mail\lib\classes\Swift\ByteStream\FileByteStream.php on line 48

Form HTML: Please note that action= is added via JS so thats why you don't see it.

<form class="form--square form-email bg--secondary" enctype="multipart/form-data" data-success="Thanks for your enquiry, we'll be in touch soon" data-error="Please fill all required fields" method="POST">
              <h3>Work With Us</h3>
              <p>Are you clever and fascinated by solving complex problems and interested in a career in cyber security?
                 Interested in joining a company on the forefront of changing the cybersecurity landscape?
                 Want to be part of a fun and exciting environment?</p>

              <div class="input-with-icon col-sm-12">
                  <i class="icon-MaleFemale"></i>
                  <input class="validate-required" type="text" name="name" placeholder="Your Name" />
              </div>
              <div class="input-with-icon col-sm-6">
                  <i class="icon-Email"></i>
                  <input class="validate-required validate-email" type="email" name="email" placeholder="Email Address" />
              </div>
              <div class="input-with-icon col-sm-6">
                  <i class="icon-Phone-2"></i>
                  <input type="tel" name="telephone" placeholder="Phone Number" />
              </div>
              <div class="col-sm-12 text-center">
                <div class="input-file">
                  <label>Attach Your Resumé</label>
                  <input class="validate-required validate-file" type="file" name="fieldAttachment" />
                </div>
              </div>
              <div class="col-sm-4 col-sm-push-4">
                  <button type="submit" class="btn btn--primary">
                      Submit
                  </button>
              </div>
          </form>

Mail.php

    <?php

   /*----------------------------------------------------------------------------*\
  |*  Email settings for sending all emails from your website forms.              *|
   \*============================================================================*/

  // Choose here whether to use php mail() function or your SMTP server (recommended) to send the email.
  // Use 'smtp' for better reliability, or use 'phpmail' for quick + easy setup with lower reliability.
  $emailMethod                = 'phpmail'; // REQUIRED value. Options: 'smtp' , 'phpmail'

  // Outgoing Server Settings - replace values on the right of the = sign with your own.
  // These 3 settings are only required if you choose 'smtp' for emailMethod above.

  // $outgoingServerAddress      = 'mail.yourdomain.com'; // Consult your hosting provider.
  // $outgoingServerPort         = '25';                  // Options: '587' , '25' - Consult your hosting provider.
  // $outgoingServerSecurity     = 'tls';                 // Options: 'ssl' , 'tls' , null - Consult your hosting provider.
  //
  // // Sending Account Settings - replace these details with an email account held on the SMTP server entered above.
  // // These 2 settings are only required if you choose 'smtp' for emailMethod above.
  // $sendingAccountUsername     = 'email@domain.com';
  // $sendingAccountPassword     = 'p@55w0rd';

  $recipients             = [ 'brianandgarcia@gmail.com' => 'Brian Garcia - Some Ventures',
                            'brian@notabilitypartners.com' => 'Brian Neat - Some Ventures']; // REQUIRED value.


  // Email details            - Change these to suit your website needs
  $emailSubject               = 'A New Resumé Was Submitted'; // REQUIRED value. Subject of the email that the recipient will see
  $websiteName                = 'Some Ventures';                // REQUIRED value. This is used when a name or email is not collected from the website form

                                                               // See a list of all supported timezones at: http://php.net/manual/en/timezones.php
   /*----------------------------------------------------------------------------*\
  |*  You do not need to edit anything below this line, the rest is automatic.    *|
   \*============================================================================*/
  include('lib/mail_sender.php');

  ?>

mail_sender.php

    <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');


    // Set default timezone as some servers do not have this set.
    if(isset($timeZone) && $timeZone != ""){
        date_default_timezone_set($timeZone);
    }
    else{
        date_default_timezone_set("UTC");
    }

    // Require the Swift Mailer library
    require_once 'swift_required.php';

    $messageText = "";

    if($emailMethod == 'phpmail'){
        $transport = Swift_MailTransport::newInstance();
    }elseif($emailMethod == 'smtp'){
        $transport = Swift_SmtpTransport::newInstance( $outgoingServerAddress, $outgoingServerPort, $outgoingServerSecurity )
        ->setUsername( $sendingAccountUsername )
        ->setPassword( $sendingAccountPassword );
    }

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

    // Creating the message text using fields sent through POST
    foreach ($_POST as $key => $value)
    {
        // Sets of checkboxes will be shown as comma-separated values as they are passed in as an array.
        if(is_array($value)){
            $value = implode(', ' , $value);
        }
        $messageText .= ucfirst($key).": ".$value."\n\n";
    }

    if(isset($_POST['email']) && isset($_POST['name']) ){
        $fromArray = array($_POST['email'] => $_POST['name']);
    }else{ $fromArray = array($sendingAccountUsername => $websiteName); }

    $message = Swift_Message::newInstance($emailSubject)
      ->setFrom($fromArray)
      ->setTo($recipients)
      ->setBody($messageText)
        ->attach(Swift_Attachment::fromPath($_POST['fieldAttachment']['tmp_name'])->setFilename($_POST['fieldAttachment']['name']));

    // Send the message or catch an error if it occurs.
    try{
        echo($mailer->send($message));
    }
    catch(Exception $e){
        echo($e->getMessage());
    }

    exit;
    ?>

Thanks so much, help is always appreciated.

Brian Garcia
  • 35
  • 1
  • 8
  • It should be `$_FILES`, not `$_POST['fieldAttachment']`. A simple `print_r($_POST)` would have helped you debug this. – Blue Jul 25 '18 at 20:18
  • Hey thanks for your help. But, I'm still getting an error https://pastebin.com/uumqWSJh . – Brian Garcia Jul 25 '18 at 20:39
  • `print_r($_POST);` and `print_r($_FILES)`. Make sure you're accessing the correct elements, and that `tmp_name` is set correctly. – Blue Jul 25 '18 at 20:43

0 Answers0