title: How to send emails with PHP using the PEAR Mail package with attachment
body: <p>I am trying to send an email with PHP by using the PEAR mail package with an attachment. The email sends successfully with a code I got from the internet. However, the attachment does not get sent or attached. Where am I going wrong, below is my code.</p>
  
  <pre><code>&lt;?php
  
  require_once "Mail.php"; 
  require_once "Mail/mime.php";
  
  $from = "&lt;my.name@company.com&gt;";
  $to = "&lt;myname@gmail.com&gt;";
  $subject = "Testing email from PHP with attachment";
  $body = "Testing email from PHP with attachment";
  $file = "invoices/PRINV7_3.pdf";
  $host = "host";
  $port = "25";
  $username = "username";
  $password = "password";
  
  $headers = array ('From' =&gt; $from,
    'To' =&gt; $to,
    'Subject' =&gt; $subject);
  
  $mime = new Mail_mime();
  
  if ($mime-&gt;addAttachment($file,'application/pdf')){
      echo "attached successfully! &lt;/br&gt;";
  } else {
      echo "Nope, failed to attache!! &lt;/br&gt;";
  }
  
  $smtp = Mail::factory('smtp',
    array ('host' =&gt; $host,
      'port' =&gt; $port,
      'auth' =&gt; true,
      'username' =&gt; $username,
      'password' =&gt; $password));
  
  $mail = $smtp-&gt;send($to, $headers, $body);
  
  if (PEAR::isError($mail)) {
    echo("&lt;p&gt;" . $mail-&gt;getMessage() . "&lt;/p&gt;");
   } else {
    echo("&lt;p&gt;Message successfully sent!&lt;/p&gt;");
   }
  
  ?&gt;
  </code></pre>