title: PHP PEAR Mail and Mail_mime adding attachments to email send with SMTP
body: <p>I am having trouble sending emails with the PHP PEAR Mail and Mail_mime with attachments.</p>
  
  <p>The problem is not sending the email itself the email gets sent just fine its just that the resulting email is a gibberish like mixe of characters.</p>
  
  <p>Here is the content of my test.php(I remove what is not nedded for the question):</p>
  
  <pre><code>&lt;?php
  //All the custom classes needed for the whole code but I dont use them all for this exemple.
  use Framework\cUsager;
  use Framework\cConnexion;
  use Framework\cPHPMailer;
  use Framework\cLog;
  use Framework\cFileStream;
  use Framework\gConfig as g;
  
  require_once "Config.php";
  
  $HTML = "&lt;div&gt;This is a test&lt;/div&gt;";
  $Text = "Ceci est un grand grand test";
  
  $mail = new cPHPMailer
  (
      'To@exemple.com',
      'From@exemple.com',
      'Test',
      'test.txt',//this is a text file a want to send as attachment, its save in the same directory as test.php
      '1.0',
      'text/html; charset=UTF-8',
      $HTML,
      $Text
  );
  
  $mail-&gt;Mail();
  ?&gt;
  </code></pre>
  
  <p>And here is the code for cPHPMailer:</p>
  
  <pre><code>&lt;?php
  namespace Framework;
  header('Content-Type: text/html; charset=UTF-8');
  require_once "Mail.php";
  require_once "Mail/mime.php";
  
  class cPHPMailer
  {
  //I skipped all the getters and setters as well as the __construct and the attributs you guys can guess that $this-&gt;getTo() return to the To attributes in my class.
      public function Mail()
      {
          $header = array
          (
              "To" =&gt; $this-&gt;getTo(),
              "From" =&gt; $this-&gt;getFrom(),
              "Subject" =&gt; $this-&gt;getSubject(),
              "MIME-Version" =&gt; $this-&gt;getMimeVersion(),
              "Content-Type" =&gt; $this-&gt;getContentType()
          );
  
          $mime = new \Mail_mime();
  
          $mime-&gt;setTXTBody($this-&gt;getText());
          $mime-&gt;setHTMLBody($this-&gt;getHTML());
  
          foreach($this-&gt;getAttachments() as $att)
          {
              if($mime-&gt;addAttachment($att))
              {
                  echo "Att: ".$att."&lt;br&gt;";
              }
              else
              {
                  echo "TEST FAILED!!!!!";//THIS IS EDIT #2
              }
          }
  
          $smtp = \Mail::factory
          (
              'smtp', 
              array
              (
                  'host' =&gt; gConfig::$SMTPMailHost,//those are store in the Config.php file in test.php (see code above)
                  'port' =&gt; gConfig::$SMTPMailPort,
                  'auth' =&gt; gConfig::$SMTPMailAuth,
                  'username' =&gt; gConfig::$SMTPMailUser,
                  'password' =&gt; gConfig::$SMTPMailPass
              )
          );
  
          $envoyer = $smtp-&gt;send($this-&gt;getTo(),$mime-&gt;headers($header),$mime-&gt;get());
  
          echo $envoyer;//this return true/1 meaning that no errors occured
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Here is the content of the email I receive when running the script:</p>
  
  <blockquote>
    <p><code>--=_88e78c51e29d6b210a754d69484afcbc Content-Type: multipart/alternative; boundary="=_2ceac717c96dcbe1a49c37c533389352" --=_2ceac717c96dcbe1a49c37c533389352 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1 Ceci est un grand grand test --=_2ceac717c96dcbe1a49c37c533389352 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
    This is a test
    --=_2ceac717c96dcbe1a49c37c533389352-- --=_88e78c51e29d6b210a754d69484afcbc Content-Transfer-Encoding: base64 Content-Type: application/octet-stream; name=test.txt Content-Disposition: attachment; filename=test.txt Y2VjaSBlc3QgdW4gdGVzdA== --=_88e78c51e29d6b210a754d69484afcbc--</code></p>
  </blockquote>
  
  <p>I was hoping to receive an email with the body either <code>&lt;div&gt;this is a test&lt;/div&gt;</code> or 'Ceci est un grand grand test' with a text file attach named test.txt.</p>
  
  <p><strong>EDIT</strong>:
  After removing the error suppression operator I received no warning or error.</p>
  
  <p><strong>EDIT</strong>:
  I had a flash, I changed the foreach loop add a If statement to see if the attachment get attach correctly and it does not throw any error it seems to get attach correctly.</p>
  
  <p>results of the echoes:</p>
  
  <pre><code>Att: test.txt
  1
  </code></pre>