Hi
Excuse me, I am looking for this subject in drupal.org, but I don't find a good solution.
How can I attach a file with an email?
Please help me for the best solution.

Comments

cog.rusty’s picture

Your question could mean anything. I guess you are not asking how to click the "Attach a file" button in your email program or in your web mail provider's page. You will need to explain what are you trying to do.

If you want to create Drupal posts using emails with files attached, check the modules http://drupal.org/project/mailhandler and http://drupal.org/project/mailsave

gr8cms’s picture

thank you for your answer
yes, I want to do attach a file(.doc, .docx or .pdf) with my email.
please give me a simple and good solution.
your links was not useful for me.

cog.rusty’s picture

Sorry to hear that.

anbarasan.r’s picture

Instead of calling drupal_mail function call the following function and pass your attachment file paths as an array
function call should be like this
$attachments[0]='Your file path';
drupal_mail_wrapper_attachment('user-register', $user_obj->mail, $subject, $body, null, $headers, $attachments);

/*
* This function send mail with attachement
* 
*/
function drupal_mail_wrapper_attachment($mailkey, $to, $subject, $body, $from, $header,$attachment="") {
 $mail = new phpmailer(); //Create a new phpmailer object.
 $username = variable_get('smtp_username', '');
 $password = variable_get('smtp_password', '');
 
 //Set the default name emails should be from. If value is not defined in settings use site_name.
 if (variable_get('smtp_fromname', '') != ''){
    $from_name = variable_get('smtp_fromname', '');
 }
 else{
   $from_name = variable_get('site_name', '');
 }
 
  //If from email address is blank use smtp_from config option
  if ($from == '' && variable_get('smtp_from', '') != ''){
    $from = variable_get('smtp_from', '');
  }
 
 //Decide whether to use SMTP Authorization. Do so if username and password are given.
 if ($username != '' and $password != '') {
   $auth = TRUE;
 } else {
   $auth = FALSE;
 }
 
 
 //Take care of the email headers.
  foreach ($header as $key => $value) {
    //watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
    if (strtolower($key) == 'from') {
      if ($from == NULL or $from == '') {
        $from = $value; //If a from value was already given then set based on header.
      }
    }
    else if (strtolower($key) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
      $mail->IsHTML(TRUE);
    }
    else if (strtolower($key) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
      //$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
      $mail->AddCustomHeader($key . ": " . $value);
      $mail->message_type = "pre";
    }
    else if (strtolower($key) == 'reply-to') {
      $mail->AddReplyTo = $value;
    }
    else if (strtolower($key) == 'return-path') {
      if (trim($value) !=  '') {
        $mail->Sender = $value;
      }
    }
    else if (strtolower($key) == 'content-transfer-encoding') {
      $mail->Encoding = $value;
    }
    else if (strtolower($key) == 'mime-version') {
      // just ommit MIME-Version it since it will be set by PHP-Mailer
    }
    else if (strtolower($key) == 'bcc') {
      $bccrecipients = split(",", $value);
      foreach ($bccrecipients as $bccrecipient) {
        if ( strpos($bccrecipient, '<') !== false ) {
          $bccparts = explode(" <", $bccrecipient);

          $bccname = $bccparts[0];
          $bccaddr = rtrim($bccparts[1], ">");
        }
        else {
          $bccname = "";
          $bccaddr = $bccrecipient;
        }
        $mail->AddBCC($bccaddr, $bccname);
      }
    }
    else { //Else the header key is not special, just add it.
      $mail->AddCustomHeader($key . ": " . $value); //Add header line.
    }
  }
 
 //If no from address has been set than use a default.
 if ($from == '' or $from == NULL){
    $from = variable_get('site_mail', 'test@example.com'); //If no from can be found use site_mail variable.
 }
 
 //Set the correct protocol prefix to append to the smtp host.
 switch(variable_get('smtp_protocol', 'standard')){
   case "ssl":
     $mail->Protocol = 'ssl://';
     break;
   case "tls":
     $mail->Protocol = 'tls://';
     break;
   case "standard":
     $mail->Protocol = '';
 }
 
 $mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
 $mail->Port = variable_get('smtp_port', '25');
 $mail->Mailer = "smtp";
 $mail->SMTPAuth = $auth;
 $mail->Username = $username;
 $mail->Password = $password;
 $mail->CharSet = 'utf-8';
 
 $mail->From = $from;
 $mail->FromName = $from_name;
 
  $torecipients = split(",", $to);
  foreach ($torecipients as $torecipient) {
    if (strpos($torecipient, '<') !== false) {
      $toparts = explode(" <", $torecipient);
      $toname = $toparts[0];
      $toaddr = rtrim($toparts[1], ">");
    }
    else {
      $toname = "";
      $toaddr = $torecipient;
    }
    $mail->AddAddress($toaddr, $toname);
  }
 
 $mail->Subject = $subject;
 $mail->Body = $body;
 if(count($attachment)) {
 foreach ($attachment as $attachments)  {
  //$target_path_pdf = file_directory_path().'/bill/new/meha.pdf';
  $mail->AddAttachment($attachments); // attachment
 }
 }
 watchdog('smtp', "Sending mail to: $to");
 //Try to send email, if it fails set watchdog entry.
 if(!$mail->Send()) {
    watchdog("smtp", "Error sending email: '". $mail->ErrorInfo ."' From: '$from' To: '$to'", WATCHDOG_ERROR);
  return false;
 } 
 $mail->SmtpClose();
 return true;
}
gr8cms’s picture

Excuse me, can u tell about attachment file?
It must be select from local or host?

anbarasan.r’s picture

from the host only
Ex:
$attachments[0]= file_directory_path().'/bill/new/meha.pdf';

gr8cms’s picture

two more question:
1- How about if I want to email a file directly selected by the user from his computer(I mean a file that is located on a local computer).
2- and how can I include phpmailer class in my module?
thanks in advance.

anbarasan.r’s picture

You have to move that file to server then only we can attach to that mail and also you need to enable SMTP module which requires phpmailer so you can download it from the following URL or from some other location also and put it into the smtp module folder.

gr8cms’s picture

I did something that you told, but I have an error like: "Fatal error: Class 'phpmailer' not found in"
please, help me
thanks in advance

anbarasan.r’s picture

i given that function for drupal5 if you are using drupal6 then you just replace $mail = new phpmailer(); with the following line i hope you have placed the phpmailer inside the smtp module.

  require_once(drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php');

  // Create a new PHPMailer object.
  $mail = new PHPMailer();

mboc’s picture

hi thanks for the function!
My smtp is working (the test mail), but when I use the function I'm getting "Error sending email:SMTP Error: Could not authenticate."
any ideas where this could be comming from?

revnoah’s picture

We encountered a frustrating bug related to encryption protocols. We found that the SMTPSecure property needed to be set to send attachments through gmail using TLS and SSL. A slight modification was required to the code above.

	//Set the correct protocol prefix to append to the smtp host.
	switch(variable_get('smtp_protocol', 'standard')){
	   case "ssl":
	     $mail->Protocol = 'ssl://';
		 $mail->SMTPSecure = 'ssl';
	     break;
	   case "tls":
	     $mail->Protocol = 'tls://';
	     $mail->SMTPSecure = 'tls';
	     break;
	   case "standard":
	     $mail->Protocol = '';
	     break;
	}