title: PHP: How to send email with attachment using smtp settings?
body: <p>I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?</p>
  
  <pre><code>require_once "Mail.php";
  
  $from = "Usman &lt;from@example.com&gt;";
  $to = "Naveed &lt;to@example.com&gt;";
  $subject = "subject";
  $body = "";
  
  $host = "smtp.gmail.com";
  $username = "username";
  $password = "password";
  
  $headers = array ('From' =&gt; $from,
        'To' =&gt; $to,
        'Subject' =&gt; $subject);
  
  $smtp = Mail::factory( 'smtp', array('host' =&gt; $host,
            '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;" );
  }
  </code></pre>