Background
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.
Problem
I'm trying to implement this script, which will send emails with attachments, the relevant blog is here.
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.
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' => 'dom.txt',
'filename' => 'dom.txt',
);
print_r($attachment);
$email = new AttachmentEmail($to, $from, $subject, $message, $attachment);
$email->send();
The email arrives fine but the attachment doesn't work and I get the following errors.
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
I think I'm doing something wrong with the path but I have no idea what. What am I doing wrong?
What I've Tried
'path' => 'dom.txt',
'path' => './dom.txt',
'url' => 'http://localhost/tutorials/dom.txt',
And all sorts of variations like that, but I'm blundering really.
I started printing out variables from public function get_binary_attachments()
in the script above to see where it's going wrong and printing $attachment gives:
Array ( [url] => ./dom.txt
[filename] => dom.txt )
But $attachment_bin is empty, so something breaks there. But it might still be related to path.
EDIT --
Here is the relevant part of the script:
73 public function get_binary_attachments() {
74 $output = '';
75 foreach($this->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->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}
EDIT -- New Solutions tried:
Changing attachment to the following array add's three new lines of errors :
'path' => 'dom.txt',
'type'=>'text/plain',
'filename' => 'dom.txt',
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
Using 'path' => './dom.txt'
, changes the file_get_contents errors to:
Warning: file_get_contents(.): failed to open stream: Permission denied in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
Changing the array to below, also does nothing.
'path'=>'./dom.txt',
'type'=>'MIME/type'
It is failing to find your attachments (it is looking for them in C:\XAMPP\htdocs\tutorials\
). Where are your files located in relation to the script? If they were in your root directory, you should be able to prefix the attachment path with something like this:
$attachment = array(
'url' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
'filename' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
);