Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By paul.linney on
Hi
I am hoping some one can help me with this snippet of code for sending emails with an attachment. I have added the MimeMail module to handle the email attachment encoding, but it never attaches the file.
I have tried using the raw $file->uri (public://) and the drupal_realpath($file->uri) but none of them seem to work.
I'm not even sure if its invoking the MimeMail module although my `variable_get('mail_system')` says its set to `MimeMailSystem`
Can anyone check the code below and advise of any errors/ommissions?
$file = file_load($form_state['values'][0]);
$message = array(
'headers' => array('Content-Type' => 'text/html'),
'key' => 'test',
'to' => 'paul.linney@weareroobarb.com',
'from' => 'info@weareroobarb.com',
'subject' => 'Test email',
'body' => '<h1>Test Email</h1></p>This is a test email with HTML formats and styling</p>
<dl><dt>Bulleted list</dt><dd><ul><li>Item one</li><li>Item two</li><li>Item three</li></ul></dd>
<dt>Ordered list</dt><dd><ol><li>Item one</li><li>Item two</li><li>Item three</li></ol></dd>
<dt>Table</dt><dd><table><thead><tr><th>Left column header</th><th>Right column header</th></tr></thead>
<tbody><tr><th>First row header</th><td>First row data</td></tr>
<tr><th>Second row header</th><td>Second row data</td></tr></tbody></table></dd></dl>'
);
$message['attachments'][] = array(
'filepath' => $file->uri,
'filename' => $file->filename,
'filemime' => $file->filemime,
'list' => TRUE
);
$system = drupal_mail_system('mimemail', 'test');
$system->format($message);
$result = $system->mail($message);
regards,
Paul
Comments
Same problem
I am looking for the same answer.
I also tried 'filepath' => file_uri_target($file->uri), but without success.
I got so p**sed off with the
I got so p**sed off with the MimeMail module in Drupal 7 I went down another route that 100% works:
Then use it like this:
Hope that helps
Another Route, SwiftMail
Hi,
Its frustrating as I can't understand why it doesn't work. However my solution was to take the burden off Drupal completely by using SwiftMail.
I dumped the SwiftMail into a folder in my theme and then used the following:
Paul
Paul Linney
Owner & Web Developer
Error sending attachment, but still sending
I use both the mimemail and mailsystem and it sends the attachment but gives and error on top of confirmation section, error below:
It always sends the two lines together. Any ideas??
Following is the code from
Following is the code from isos adapted for multiple attachments
Attachments are now an array with multiple file arrays
This works for me
Instead of putting your attachments in
$message['attachments'][]
try adding them to
$message['params']['attachments'][]
Have a look at the updates on
Have a look at the updates on #1389504: Documentation for sending emails with attachments
http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes
Also works for me
Thanks skcuslapurd, your solution works for me.