0

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.

3

For sending mail with attachment you should use Mail System and Mime Mail module Attachment functionality is built into the Mime Mail module.

After that you must set up drupal_mail() to use mime mail as the mail system interface. This is where the mail system module comes into play. The module is pretty straightforward, and lets you set a different mail system per module. There is a straightforward configuration at admin/config/system/mailsystem where you create a new setting for your module, and select mime mail as the mail system.

Assing anttatcment in variable

$message['params']['attachments']

After that you will be able to send mail with attachment.

For more information visit here

1
  • Thank you very much, i installed the mime mail module and the above functions worked finely without any modification. – mohammed amine bourkadi Apr 29 '14 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.