开发者

Sending email with attachment

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?


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.


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.


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.


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.


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜