title: Sending an email with an attachment via PHPMailer
body: <p>I'm having trouble getting PHPMailer to send out an email with an image attachment that a user submits. I just keep getting "Mail Error" everytime it tries to send. I just can't see what is going wrong. There's no errors in the error_log, it just fails everytime.</p>
  
  <p>HTML Input dealing with image attachment:</p>
  
  <pre><code>&lt;input type="file" name="file" placeholder="Choose an image." id="imageupload" accept="image/jpeg" /&gt;
  </code></pre>
  
  <p>PHP Code:</p>
  
  <pre><code>&lt;?php
  
  // Make sure logged in
  include('../php_scripts/login-required.php');
  // Make sure posted
  if (!$_POST) {
      header('location: ./404.php');
  }
  // Include PHPmailer
  require 'site_includes/PHPMailer/class.phpmailer.php';
  
  // Get variables
  $offertype = $_POST['ot'];
  if ($offertype == 1) {
      $offertype = 'Discount';
  } elseif ($offertype == 2) {
      $offertype = 'Free';
  } else {
      $offertype = 'Not set';
  }
  $offerdetail = $_POST['od'];
  // Check for errors
  if (!isset($offertype) || empty($offertype)) {
      $errors = 1;
      header('location: ./create-advert.php?errors=' . $errors . '&amp;ot=' . $offertype . '&amp;od=' . $offerdetail);
  } elseif (!isset($offerdetail) || empty($offerdetail)) {
      $errors = 2;
      header('location: ./create-advert.php?errors=' . $errors . '&amp;ot=' . $offertype . '&amp;od=' . $offerdetail);
  }
  
  // Get variables from database
  include('../php_scripts/db-connect.php');
  include('../php_scripts/get-userinfo.php');
  
  // Set mail variables
  $reply      = $email;
  $replyname  = $fullname;
  $to         = 'test@example.com';
  $toname     = 'John Doe';
  $from       = 'example@example.com';
  $subject    = 'New Advert Request';
  $message    = 'Name: ' . $fullname . "\r\n" . "Company: " . $company . "\r\n" . "Phone: " . $phone . "\r\n" . "Email: " . $email . "\r\n\r\n" . $offertype . "\r\n" . "--------------------------------------------------" . "\r\n\r\n" . $offerdetail;
  
  if (isset($_FILES['file'])) {
  
      // Get attachment and upload
      $tmp_name = $_FILES['file']['tmp_name'];
      $name = $_FILES['file']['name'];
      move_uploaded_file($tmp_name, "uploads/$name");
  
      // Send email with attachment
      $mail = new PHPMailer;
  
      $mail-&gt;From = $from;
      $mail-&gt;addAddress($to, $toname);
      $mail-&gt;addReplyTo($reply, $replyname);
      $mail-&gt;WordWrap = 50;
      $mail-&gt;addAttachment(
          '/uploads/' . $name,
          $name,
          'base64',
          'mime/type'
      );
      $mail-&gt;Subject = $subject;
      $mail-&gt;AltBody = $message;
  
      if (!$mail-&gt;send()) {
          echo 'Mail Error!';
          exit;
      } else {
          echo 'Message has been sent!';
      }
  }
  </code></pre>
  
  <p>?></p>
  
  <p>I also know I should be creating an array for acceptable  file extensions (only .jpg or .jpeg in this case), but am also unsure of exactly how and where to implement these.</p>
  
  <p>Also, I only changed the $to and $from emails. Why would this always be failing to send? All I'm looking to do is send a plain text email either with an email attachment or without one.</p>