views:

244

answers:

3

I've a custom form (created with form API) that need send an uploaded file by email. The current form submit handler send the email withouth attachment using drupal_mail().

So I'm looking for a solution to properly send email with attachment from Drupal. Mime Mail seems overkill because HTML mail, templating and its other features are not required. But the only other alternative I see is to set the appropriate headers and serialize the attached file in the mail body when processing the mail in my hook_mail implementation.

Did I miss anything ? Is there some module to handle this ?

A: 

The Webform module allows you to create a form and has a file option which can be used as an attachment. All available form components are listed on the module's manual page.

Once installed Webform will appear as a content type. Once you have saved the fundamentals, such as the title and the email to address, you will have the ability to add the required form components.

Add a component of type 'file', ensuring the 'email' (to recipient) option is ticked, and you will then be able to customize the permitted file types, extensions, sizes and upload folder.

CitrusTree
Yep, I know Webform can do it. But since this is a single developer-build form not the be modified by the end-user of the site, it doesn't fit the basic Webform use case. I would like to avoid having to bring the whole UI-editable forms stack of Webform for a single form that has also other process (various DB update) to do.
mongolito404
Now you tell us ;0)
CitrusTree
Of course you could simply not give the other users the permission to administer Webforms :0)
CitrusTree
+1  A: 

Mimemail is the easiest solution here. Be it an overkill or not, it will allow you to get it done with a single function call.

If you insist, you may have your homemade attachment sender: base64 encode your attachment(s), add them to the mail body, add the correct headers and you're done.

Omar
+1  A: 

How about require() ZEND framework and doing this?

function sendEmail($params){

ini_set('include_path', 'inc/');
require_once ('inc/Zend/Mail.php');

$mail = new Zend_Mail();

$mail->setSubject( $params['subject'] );

$mail->setBodyText( $params['bodyText'] );
$mail->setBodyHtml( $params['bodyHtml'] );

$mail->setFrom( $params['fromEmail'], $params['fromName'] );
$mail->addTo( $params['toEmail'], $params['toName'] );

// Finally, add an attachment

assert( file_exists($params['attachFile']) );

$at = $mail->addAttachment(file_get_contents($params['attachFile']));
$at->type        = $params['attachType'];
$at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$at->filename    = $params['attachName'];

$mail->send();

}

Stefan Pantke