5

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

So I'm looking for a solution to properly send email with attachment from Drupal. Mime Mail seems an 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 any module to handle this?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Pierre Buyle
  • 4,883
  • 2
  • 32
  • 31
  • 1
    I just want to add that the mail will be sent as a MIME Mail anyways when you attach files. So the HTML part is not "overkill", it's probably just the same code as the attachment code. – Emil Vikström Aug 22 '10 at 15:00
  • I don't need/want the body of the mail to be in HTML, I only need/want plain-text email with attachments. Anything more is out of scope and I don't want to be required to deal with it. – Pierre Buyle Aug 22 '10 at 15:15
  • depending on the outcome here: http://meta.stackexchange.com/questions/110752/should-we-migrate-questions-to-an-on-topic-site-if-the-asker-requests you may decide to flag it again. Dismissing for now. – Sam Saffron Nov 01 '11 at 04:19

5 Answers5

4

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 Ali
  • 8,467
  • 4
  • 33
  • 58
1

You could always have a look at the Swift Mailer module which lets you send HTML (MIME) e-mails, e-mails with inline images and e-mails with attachments. It is also cabable of automatically generating plain text versions based on the HTML e-mail version, which in the end will let the user's e-mail client display the preferred version (HTML or plain text).

The Swift Mailer module is available on http://drupal.org/project/swiftmailer.

For the record : I'm the author and maintainer of the module.

sbrattla
  • 5,274
  • 3
  • 39
  • 63
1

You can use mime mail and force the message body to be sent in plaintext format. Here is an excerpt from the module's readme file:

USAGE This module may be required by other modules, but is not terribly useful by itself. Once installed, any module can send messages by calling the mimemail() function:

$sender - a user object, text email address or an array with name, mail
$recipient - a user object, text email address or an array with name, mail
$subject - subject line
$body - body text in HTML format
$plaintext - boolean, whether to send messages in plaintext-only (default FALSE)
$headers - a keyed array with headers (optional)
$text - plaintext portion of a multipart e-mail (optional)
$attachments - array of arrays with the file's path, MIME type (optional)
$mailkey - message identifier

return - an array containing the MIME encoded message

The key thing being to set the $plaintext argument to TRUE. Now you can have your cake and eat it too.

0

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
  • 184
  • 2
  • 10
  • 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. – Pierre Buyle Aug 22 '10 at 15:11
  • Of course you could simply not give the other users the permission to administer Webforms :0) – CitrusTree Aug 22 '10 at 15:18
0

You could use the Zend Framework.

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();
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
SteAp
  • 11,853
  • 10
  • 53
  • 88