Thanks for the hints.

I'm maintaining the code done by another developer:


from django.conf import settings
from django.template import Context, Template
from django.utils.encoding import force_unicode

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from smtplib import SMTP
import email.Charset


charset='utf-8'
smtp_server='XXXXXXXXXXX'
smtp_user='XXXXXXXXX'
smtp_pass='XXXXXXXXX'

email.Charset.add_charset( charset, email.Charset.SHORTEST, None,
None )

def html_mail(subject, recipient, html_contents, text_contents,
sender=settings.DEFAULT_FROM_EMAIL, recipient_name='', sender_name='',
charset=charset):
   '''
   if you want to use Django template system:
      use `msg_context` and optionally `textmsg_context` as template
context (dict)
      and define `html_template` and `text_template` variables.
   (previously otherwise) msg and textmsg_context variables are used
as html and text message sources.
   '''
   text=None
   html=force_unicode(html_contents)
   if text_contents: text=force_unicode(text_contents)

   msg_root = MIMEMultipart('related')
   msg_root['Subject'] = force_unicode(subject)
   msg_root['From'] = named(sender, sender_name)
   msg_root['To'] =  named(recipient, recipient_name)
   msg_root.preamble = 'This is a multi-part message in MIME format.'

   msg_alternative = MIMEMultipart('alternative')
   msg_root.attach(msg_alternative)

   if text: msg_alternative.attach(MIMEText(text, _charset=charset))
   msg_alternative.attach(MIMEText(html, 'html', _charset=charset))

   smtp = SMTP()
   smtp.connect(smtp_server)
   if smtp_user: smtp.login(smtp_user, smtp_pass)
   smtp.sendmail(sender, recipient, msg_root.as_string())
   smtp.quit()

def named(mail,name):
   if name: return '%s <%s>' % (name,mail)
   return mail


As you see, after your comments I added the force_unicode function
call, but it didn't fix the problem. I tried to look into Django code
but I got a bit lost. Can you easily spot the issue in the code above.

Thank you!

Julien


On Apr 19, 1:52 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Fri, 2008-04-18 at 16:18 -0700, Julien wrote:
> > Hi,
>
> > We have a newsletter app which sends a bulk of a thousand emails every
> > fortnight to our subscribers. Subscribers can choose to receive the
> > newsletter in HTML of plain text format. The name of each subscriber
> > is added at the top of the message (e.g. "Dear Mr. Smith, blabla").
>
> > One of our subscriber has a name with special characters, 'č' and 'ć',
> > and the sending always fails. I get the error:
>
> > exceptions.UnicodeEncodeError - 'ascii' codec can't encode character
> > u'\u010d' in position 171: ordi
>
> > I'm using smtp.sendmail(). Is there any way to send an email including
> > that sort of characters?
>
> Django's trunk handles UTF-8-encoded data and unicode data transparently
> in email (see [1] for some early evidence of this).
>
> [1]http://flickr.com/photos/malcolmtredinnick/503502258/
>
> If you are asking whether you can do it with Python's raw mail handling
> libraries, then, obviously, "yes" (since Django does it) and you could
> look at how Django's source does it for some clues.
>
> Regards,
> Malcolm
>
> --
> If at first you don't succeed, destroy all evidence that you 
> tried.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to