If you are confused and trying to figure out how to just simply send a mail with and attachment using drupal_mail(), with a given $body and $subject, this below code may help you.
/*hook_mail($key, &$message, $params);*/
//for detail information please visit https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_mail/7
<?php
/**
Send Email with an attachment
*/
function custom_mail($key, &$message, $params) {
if (isset($params['subject'])) {
$message['subject'] = $params['subject'];
}
if (isset($params['body'])) {
$message['body'][] = $params['body'];
}
if (isset($params['headers']) && is_array($params['headers'])) {
$message['headers'] += $params['headers'];
}
if (isset($params['attachment'])) {
$message['params']['attachments'][] = $params['attachment'];
}
$message['headers']['Reply-To'] = 'abc@yahoo.in';
}
$from = 'NJDP Error Log File';
$body = 'hello World!';
$subject = 'PHP Error Report Mail with Attachment';
$filepath = DRUPAL_ROOT.'/php_errors.log';
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
drupal_set_message($filepath);
$attachment = array(
'filecontent' => file_get_contents($filepath),
'filename' => 'php_errors.log',
'filemime' => 'application/pdf',
);
$message = drupal_mail('custom', 'register_mail', $to, user_preferred_language($user), array('body' => $body, 'subject' => $subject, 'attachment' => $attachment), $from, TRUE);
0 Comment(s)