I noticed that when I send an attachment to a Yahoo! mail account, attachments weren't being received.
I checked the headers and saw that I was sending Content-type: multipart/alternative when Yahoo! demanded Content-type: multipart/mixed. Other mail providers, e.g. Gmail, don't seem to care (h/t http://stackoverflow.com/questions/10873269/correct-email-format-for-ema...).
I'm not 100% sure that this is a Mail MIME issue, but it is the only module I'm using that touches on the Content-type headers at all. The parseDecoded function in mailmime.inc doesn't seem to have the capability to add a 'multipart/mixed' header to the MIME object.
My code for sending the attachments goes:
function my_module_send_email($to, $subject, $message, $attachments = array()) {
$params = array(
'subject' => $subject,
'message' => $message,
);
$message = drupal_mail('my_module', $key, $to, 'und', $params, NULL, FALSE);
$mime = &$message['MailMIME'];
if (sizeof($attachments)) {
foreach ($attachments as $file) {
$attachment = array();
$attachment['content'] = file_get_contents(drupal_realpath('private://' . $file->filename));
$attachment['filename'] = $file->filename;
$attachment['filemime'] = $file->filemime;
$attachment['encoding'] = 'base64';
$attachment['disposition'] = 'attachment';
$attachment['charset'] = '';
$attachment['language'] = '';
$mime->addAttachment(
$attachment['content'],
$attachment['filemime'],
$attachment['filename'],
FALSE,
$attachment['encoding'],
$attachment['disposition'],
$attachment['charset'],
$attachment['language']
);
}
}
if (method_exists($mime, 'get')) {
$message['body'] = $mime->get();
}
$system = drupal_mail_system(NULL, NULL);
$message['result'] = $system->mail($message);
return $message['result'];
To get the correct header, I added some lines right before the call to $system->mail:
if (sizeof($attachments)) {
$message['headers']['Content-Type'] = str_replace('alternative', 'mixed', $message['headers']['Content-Type']);
}
So possibly this is a bug, and possibly I am just sending mail in a funky way. Any feedback would be helpful.
Comments
Comment #1
calebtr commentedJust updating the title a little
Comment #2
salvisAFAIK, multipart/alternative is for sending text/html and text/plain versions of a message, and multipart/mixed is for adding attachments.
Please update to 7.x-2.18 and let us know whether the issue is still present.
Comment #3
calebtr commentedYes, I am sending attachments and users' clients are expecting "multitype/mixed".
I updated to 7.x-2.18 and am still getting "multitype/alternative".
Comment #4
salvisI don't know what's going on here. It seems that others are using Mail MIME successfully.
To all: Are you able to send HTML and plain-text formats in the same email? How do you do it?
Comment #5
calebtr commentedThanks for all of your healthy skepticism.
I figured out where the problem in my code was: I call $mime->get() to update the content but not $mime->headers to update the headers. If I can figure out where I copied this snippet from, I'll let 'em know.
As you suggest, I am successfully sending html and plain-text formats in the same email.
Comment #6
salvisThanks for following up.
Comment #6.0
salvisfixing typo