Re: Fwd: E-mail attachments

Rob Thorne <[email protected]> Sun, 29 Dec 2013 15:47:34 -0800
Newsgroups gmane.comp.php.drupal.devel
Message-ID <[email protected]>
--Apple-Mail=_538E213B-02AC-44AC-8551-08CA8CFA9DD8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=iso-8859-1

I don't see anything obviously wrong in your sample, but after comparing =
your implementation with mine from a recent project, I see a couple of =
differences:

1. You mention using the mimemail module, but are you also using the =
mailsystem module?  The simplest explanation would be that mimemail is =
not getting invoked at all.
2. If I recall correctly, some fields you can set in the "message" array =
upstream can get unset before hook_mail is called for you.  So you may =
need to put these items into your "params" array and hammer them in as =
part of your hook_mail implementation.  My code looks like this:

Sending the mail:

  $mail_params =3D array(
    'subject' =3D> $settings['subject'],
    'message' =3D> $settings['message'],
    'headers' =3D> array(
      'Content-Type' =3D> 'text/html; charset=3DUTF-8'
    ),
    'attachments' =3D> $attachments,
    'voter' =3D> $credit_app,
  );
  $mail_params['plain'] =3D FALSE;
  $mail_params['plaintext'] =3D $mail_params['message'];
  if (!empty($settings['bcc'])) {
    $mail_params['headers']['Bcc'] =3D $settings['bcc'];
  }
 =20
  $message =3D drupal_mail('mymodule', 'send-test', $address, $language, =
$mail_params, $from);

In my hook_mail implementation:

  switch ($key) {
    case 'send-test':
      $message['subject'] =3D $params['subject'];
      $message['body'][] =3D $params['message'];
      $message['headers'] =3D array_merge($message['headers'], =
$params['headers']);
      break;
  } =20

Note also that I'm setting some parameters that you are not setting =
('plain', 'plaintext');  I believe I added those after debugging the =
problem you're having here.

Hope this helps,

Rob

Rob Thorne
Torenware Networks



On Dec 29, 2013, at 12:50 PM, Muzaffer Tolga Ozses <[email protected]> =
wrote:

> Hi,
>=20
> I keep developing my module, and inside it I am sending e-mail. I am =
also trying to attach a file, to no avail. I have installed mimemail, I =
have changed mime type in my code, I have dpm'd everything. dpm gives me =
correct values, and the subject and body of the e-mail come through, but =
I couldn't get file attachment to work at all. What else can I do?
>=20
> Regards,
>=20
> PS: You can see the code at =
http://drupalcode.org/sandbox/Kartagis/1543120.git/blob/da47378:/pass2pdf.=
module#l69
>=20


--Apple-Mail=_538E213B-02AC-44AC-8551-08CA8CFA9DD8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=iso-8859-1

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Diso-8859-1"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I =
don't see anything obviously wrong in your sample, but after comparing =
your implementation with mine from a recent project, I see a couple of =
differences:<div><br></div><div>1. You mention using the mimemail =
module, but are you also using the mailsystem module? &nbsp;The simplest =
explanation would be that mimemail is not getting invoked at =
all.</div><div>2. If I recall correctly, some fields you can set in the =
"message" array upstream can get unset before hook_mail is called for =
you. &nbsp;So you may need to put these items into your "params" array =
and hammer them in as part of your hook_mail implementation. &nbsp;My =
code looks like this:</div><div><br></div><div>Sending the =
mail:</div><div><br></div><div><blockquote style=3D"margin: 0 0 0 40px; =
border: none; padding: 0px;"><div><div>&nbsp; $mail_params =3D =
array(</div><div>&nbsp; &nbsp; 'subject' =3D&gt; =
$settings['subject'],</div><div>&nbsp; &nbsp; 'message' =3D&gt; =
$settings['message'],</div><div>&nbsp; &nbsp; 'headers' =3D&gt; =
array(</div><div>&nbsp; &nbsp; &nbsp; 'Content-Type' =3D&gt; 'text/html; =
charset=3DUTF-8'</div><div>&nbsp; &nbsp; ),</div><div>&nbsp; &nbsp; =
'attachments' =3D&gt; $attachments,</div><div>&nbsp; &nbsp; 'voter' =
=3D&gt; $credit_app,</div><div>&nbsp; );</div><div>&nbsp; =
$mail_params['plain'] =3D FALSE;</div><div>&nbsp; =
$mail_params['plaintext'] =3D $mail_params['message'];</div><div>&nbsp; =
if (!empty($settings['bcc'])) {</div><div>&nbsp; &nbsp; =
$mail_params['headers']['Bcc'] =3D $settings['bcc'];</div><div>&nbsp; =
}</div><div>&nbsp;&nbsp;</div><div>&nbsp; $message =3D =
drupal_mail('mymodule', 'send-test', $address, $language, $mail_params, =
$from);</div></div><div><br></div></blockquote></div><div>In my =
hook_mail implementation:</div><div><br></div><div><blockquote =
style=3D"margin: 0 0 0 40px; border: none; padding: =
0px;"><div><div>&nbsp; switch ($key) {</div><div>&nbsp; &nbsp; case =
'send-test':</div><div>&nbsp; &nbsp; &nbsp; $message['subject'] =3D =
$params['subject'];</div><div>&nbsp; &nbsp; &nbsp; $message['body'][] =3D =
$params['message'];</div><div>&nbsp; &nbsp; &nbsp; $message['headers'] =3D=
 array_merge($message['headers'], $params['headers']);</div><div>&nbsp; =
&nbsp; &nbsp; break;</div><div>&nbsp; } =
&nbsp;</div></div><div><br></div></blockquote></div><div>Note also that =
I'm setting some parameters that you are not setting ('plain', =
'plaintext'); &nbsp;I believe I added those after debugging the problem =
you're having here.</div><div><br></div><div>Hope this =
helps,</div><div><br></div><div>Rob</div><div><br><div>
<div>Rob Thorne</div><div>Torenware Networks</div><div><br></div><br =
class=3D"Apple-interchange-newline">

</div>
<br><div><div>On Dec 29, 2013, at 12:50 PM, Muzaffer Tolga Ozses &lt;<a =
href=3D"mailto:[email protected]">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div =
dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:courier =
new,monospace">Hi,<br></div><div class=3D"gmail_quote"><div =
dir=3D"ltr"><div style=3D"font-family:'courier =
new',monospace"><br></div><div style=3D"font-family:'courier =
new',monospace">

I keep developing my module, and inside it I am sending e-mail. I am =
also trying to attach a file, to no avail. I have installed mimemail, I =
have changed mime type in my code, I have dpm'd everything. dpm gives me =
correct values, and the subject and body of the e-mail come through, but =
I couldn't get file attachment to work at all. What else can I do?</div>

<div style=3D"font-family:'courier new',monospace"><br></div><div =
style=3D"font-family:'courier new',monospace">Regards,</div><div =
style=3D"font-family:'courier new',monospace">
<br></div><div style=3D"font-family:'courier new',monospace">PS: You can =
see the code at&nbsp;<a =
href=3D"http://drupalcode.org/sandbox/Kartagis/1543120.git/blob/da47378:/p=
ass2pdf.module#l69" =
target=3D"_blank">http://drupalcode.org/sandbox/Kartagis/1543120.git/blob/=
da47378:/pass2pdf.module#l69</a></div>

</div>
</div><br></div>
</blockquote></div><br></div></body></html>=

--Apple-Mail=_538E213B-02AC-44AC-8551-08CA8CFA9DD8--