Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send Email with an attachment in Drupal 7

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 712
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: