[Patch] New functions for libs/Mail.java and mvc/Mailer.java to set character-set and etc.

Bug #474162 reported by nkmrshn
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
play framework
Status tracked in 1.0
1.0
Fix Released
Undecided
Nicolas Leroux
1.1
Fix Committed
Undecided
Unassigned

Bug Description

I made a patch for libs/Mail.java and mvc/Mailer.java to enable to,

1. set subject character-set
2. set body character-set
3. set personal, so it can be like "foo <email address hidden>" and not just "<email address hidden>".
4. add header, like "Content-Transfer-Encoding : 7bit"

The reason why I started to make these, because some of the Japanese cell phone doesn't support UTF-8.

So, after I modified the libs/Mail.java and mvc/Mailer.java, I can write like this,

 public class Mails extends Mailer {

     public static void commented(Post post) {
         setSubject("........"); // Japanese multi-byte character in UTF-8
         setSubjectCharset("iso-2022-jp"); // subject will be encoded from UTF-8 to ISO-2022-JP.
         setBodyCharset("iso-2022-jp"); // also the body too.
         setPersonal("yabe"); // "yabe <email address hidden>"
         addHeader("Content-Transfer-Encoding", "7bit"); // additional header, since ISO-2022-JP is 7bit
         addRecipient(post.author.email);
         send(post);
     }

 }

I hope that this proposal is useful.

Tags: improvement
Revision history for this message
nkmrshn (nkmrshn) wrote :
Changed in play:
assignee: nobody → Nicolas (nicolas-lunatech)
status: New → Triaged
Revision history for this message
Nicolas Leroux (nicolas-lunatech) wrote :

That is useful I think, thanks. I have a question. Why would the body charset be different from the subject charset?

The main sendmail method has too much parameters I believe, so it might be better to use a bean to construct the email, but that is another issue.

Changed in play:
status: Triaged → In Progress
Revision history for this message
nkmrshn (nkmrshn) wrote :

Yes, usually the charset of subject and body are same, but I thought that maybe there is a circumstance that both are different. So, I wanted to leave the option to set different charset.

Revision history for this message
Nicolas Leroux (nicolas-lunatech) wrote :

I have committed something that allows to your character encoding for the whole message inspired by your patch.

I also committed the additional headers from your patch file. However, I did not commit the fix for the setPersonal as you can do that using the setFrom ie setFrom("Blabla <email address hidden>").

I preserved the old methods so the API is not changed (the API is Frozen for the 1.0 branch). I only need 2 extra tests to close this issue (one for the headers and one for the encoding). Maybe you can come up with a good test case?

I also start thinking that the sendMail as too many arguments so we might want to use a Mail object to leverage the calls ie :

Mail mail = new Mail();
mail.setFrom()
mail.addHeaders();

But that is an API change and that would be for the 1.1 branch. I also don't want to end up redoing the jakarta commons email (http://commons.apache.org/email/) so we will have to see how relevant that is.

Changed in play:
status: In Progress → Fix Committed
Revision history for this message
nkmrshn (nkmrshn) wrote :

Thank you very much indeed.

I agree with you about the setPersonal, but when I looked at mvc/Mailer.java (r678) I found,

  public static void setPersonal(String personal)

I think you should remove this.

In the libs/Mail.java (r678, line 178),

  return sendMessage(buildMessage(from, replyTo, recipients, subject, body, alternate, contentType, attachments));

since you are not calling buildMessage with charset and headers variables, buildMessage function (line 210) will never called from mvc/Mailer.java. So, it should be,

  return sendMessage(buildMessage(from, replyTo, recipients, subject, body, alternate, contentType, charset, headers, attachments));

Also, addHeaders, in line 231 libs/Mail.java, MUST be after msg.setText, because setText will over write the "Content-Transfer-Encoding" header. So even if I wrote,

 addHeader("Content-Transfer-Encoding", "7bit");

the header will be, "Content-Transfer-Encoding: quoted-printable". Please move addHeaders to between line 280 and 281.

nkmrshn (nkmrshn)
Changed in play:
status: Fix Committed → Incomplete
Revision history for this message
Nicolas Leroux (nicolas-lunatech) wrote :

Thanks for your work. I will change that asap.

Changed in play:
status: Incomplete → Fix Committed
Revision history for this message
nkmrshn (nkmrshn) wrote :

I confirmed the fix on r695. Thank you.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.