title: Sending a PHP email with attachment (failure)
body: <p><strong>Background</strong></p>
  
  <p>I'm trying to send emails with attachments using a PHP script (PHP 5.4.16). Eventually I'll be trying to get it to work in Drupal, but for now it's standalone.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I'm trying to implement this <a href="https://github.com/ngmaloney/Attachment-Email/blob/master/attachment_email.php" rel="nofollow">script</a>, which will send emails with attachments, the relevant blog is <a href="http://www.metachunk.com/blog/sending-e-mails-attachments-drupal-7" rel="nofollow">here</a>.</p>
  
  <p>I'm running the following code, which calls the script above. The file it's trying to send is dom.txt in the same folder.   </p>
  
  <pre><code>require_once('attachment_email.php');
  
  $to="xxxxx@gmail.com";
  $from="xxxxx@gmail.com";
  $subject="Check out this report!";
  $message="Yo! Check out this report!";
  $attachment = array(
    'url' =&gt; 'dom.txt',
    'filename' =&gt; 'dom.txt',
  );
  
  print_r($attachment);
  
  $email = new AttachmentEmail($to, $from, $subject, $message, $attachment);
  $email-&gt;send();
  </code></pre>
  
  <p>The email arrives fine but the attachment doesn't work and I get the following errors.</p>
  
  <pre><code>Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
  Warning: file_get_contents(d): failed to open stream: No such file or directory in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
  Warning: Illegal string offset 'type' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
  Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
  Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
  Warning: file_get_contents(d): failed to open stream: No such file or directory in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
  Warning: Illegal string offset 'type' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
  Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81   
  </code></pre>
  
  <p>I think I'm doing something wrong with the path but I have no idea what. What am I doing wrong?</p>
  
  <p><strong>What I've Tried</strong> </p>
  
  <pre><code>'path' =&gt; 'dom.txt',
  'path' =&gt; './dom.txt',
  'url' =&gt; 'http://localhost/tutorials/dom.txt',
  </code></pre>
  
  <p>And all sorts of variations like that, but I'm blundering really.</p>
  
  <p>I started printing out variables from <code>public function get_binary_attachments()</code> in the script above to see where it's going wrong and printing $attachment gives:</p>
  
  <pre><code>Array ( [url] =&gt; ./dom.txt 
          [filename] =&gt; dom.txt ) 
  </code></pre>
  
  <p>But $attachment_bin is empty, so something breaks there. But it might still be related to path. </p>
  
  <p>EDIT -- </p>
  
  <p>Here is the relevant part of the script:</p>
  
  <pre><code>  73 public function get_binary_attachments() {
    74   $output = '';
    75   foreach($this-&gt;attachments as $attachment) {
    76  
    77     echo $attachment;
    78
    79     $attachment_bin = file_get_contents($attachment['path']);
    80     $attachment_bin = chunk_split(base64_encode($attachment_bin));
    81   
    82     dpm($attachment_bin);
    83     $output .= '--'. $this-&gt;boundary . PHP_EOL;
    84     $output .= 'Content-Type: '. $attachment['type'] .'; name="'. basename($attachment['path']) .'"' . PHP_EOL;
    85     $output .= 'Content-Transfer-Encoding: base64' . PHP_EOL;
    86     $output .= 'Content-Disposition: attachment' . PHP_EOL . PHP_EOL;
    87     $output .= $attachment_bin . PHP_EOL . PHP_EOL;
    88   }
    89   return $output;
    90 }
    91}
  </code></pre>
  
  <p>EDIT -- New Solutions tried:</p>
  
  <ol>
  <li><p>Changing attachment to the following array add's three new lines of errors :</p>
  
  <pre><code>'path' =&gt; 'dom.txt',
  'type'=&gt;'text/plain',
  'filename' =&gt; 'dom.txt',
  </code></pre>
  
  <p>Warning: file_get_contents(d): failed to open stream: No such file or directory in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
  Warning: Illegal string offset 'type' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
  Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81</p></li>
  <li><p>Using <code>'path' =&gt; './dom.txt'</code>, changes the file_get_contents errors to:</p>
  
  <p>Warning: file_get_contents(.): failed to open stream: Permission denied in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77</p></li>
  <li><p>Changing the array to below, also does nothing.</p>
  
  <pre><code>'path'=&gt;'./dom.txt',
  'type'=&gt;'MIME/type'
  </code></pre></li>
  </ol>