Beranda > blog > Drupal: Howto send attachment

Drupal: Howto send attachment

Sending attachment in Drupal is easy. Phpmailer is perfect to send attachments if I’m not using Drupal as the framework to build web applications. For Drupal, you can use mimemail module. Download it, extract into your sites/all/modules/ folder then activate it via Administer > Site Building > Modules.

Now the sending email part. You can specify email attachment path on your favorite folder. I prefer store it on Drupal file directory path, which is in sites/default/files. Here’s the sample code:


// define attahment paths
$filepath = variable_get('file_directory_path', 'sites/default/files').'/filesample.xls;
$attachments = array(array('filepath' => $filepath, 'filemime' => 'application/octet-stream'));
// define all variables needed to send email
$from      = 'info@example.com';
$recipient = 'test@example.com';
$subject   = 'Sample report';
$body      = '<p>Dear users,</p><p>Please find attached file for sample report bla..bla</p>';
$body     .= '<p>&nbsp;</p><p>Regards,<br />Admin</p>';

// sending email with mimemail
mimemail($from, $recipient, $subject, $body, NULL, array(), NULL, $attachments);

// log it
watchdog('sending email', 'Report has sent to %to', array('%to' => $recipient), WATCHDOG_NOTICE);

Then test it. It’s better to write it as a function so you can use it anywhere on your scripts.
Good Luck!

Categories: blog
  1. Belum ada komentar.
  1. Belum ada trackback.