I'm coding a small module allowing to send mail to new users with an attachement file as pdf. This pdf include all subscription details of the user.
I generated the pdf file using FPDF and I put it (temporary) in DRUPAL_ROOT and I used two ways to send the mail:
1) Using the hook_mail_alter
:
function aos_user_pdf_mail_alter(&$message) {
if($message['id']=='user_register_pending_approval') {
$attachment = array(
'filecontent' => file_get_contents(DRUPAL_ROOT . '/' . 'yourfilename.pdf'),
'filename' => 'yourfilename.pdf',
'filemime' => 'application/pdf'
);
$message['params']['attachments'][] = $attachment;
}
}
2) Or creating a custom function:
function aos_user_pdf_send_mail ($mail, $uid) {
$user = user_load($uid);
$to = $mail;//gets the current user's mail address
$from = variable_get('site_mail', ''); //admin's mail address
$body = 'This is a test mail';
$subject = 'Your pdf';
$attachment = array(
'filecontent' => file_get_contents(DRUPAL_ROOT . '/' . 'yourfilename.pdf'),
'filename' => 'FILE',
'filemime' => 'application/pdf',
);
$message = drupal_mail('aos_user_pdf', 'message_key', $to,
user_preferred_language($user),
array(
'body' => $body,
'subject' => $subject,
'attachment' => $attachment),
$from,
TRUE
);
}
Unfortunately the mail is sent, but without the attachement, Please can you show me where's my mistake or an alternative way to do the job.