emencia / emencia-django-newsletter

An app for sending newsletter by email to a contact list.
189 stars 72 forks source link

SMTP issues + locale #1

Closed martip closed 14 years ago

martip commented 14 years ago

In my setup (Python 2.6 on Linux) smtplib requires server port to be passed as an integer but Django keeps passing it as a long integer even if in the SMTPServer model port is defined as integer. I fixed the issue changing self.port to int(self.port) in models.py (SMTPServer.connection_valid) and changing server.port to int(server.port) in mailer.py (Mailer.smtp_connect).

I fixed other SMTP login issues by encoding username and password to 'utf-8' before passing them to smtplib:

in models.py (SMTPServer.connection_valid) I changed smtp.login(self.user, self.password) to smtp.login(self.user.encode('utf-8'), self.password.encode('utf-8'))

in mailer.py (Mailer.smtp_connect) I changed self.smtp.login(server.user, server.password) to smtp.login(server.user.encode('utf-8'), server.password.encode('utf-8'))

I don't know if these problems occur only on my machine, so I'm not submitting any patch. But if there's anyone in my situation, maybe this helps.

Last, a quick question (off-topic). I'm quite new to django. I'm translating the app to Italian (and I'll submit my language file to Transifex as soon as I'm done with it). But... where do I have to save the locale directory? I tried to add it to the installation path but it doesn't work.

Thank you very much for sharing the app. I'm beginning to use it and it looks very promising and it's saving me a lot of time.

Paolo

Fantomas42 commented 14 years ago

Hello Paolo,

I have tried to reproduce your bug but I can't. Normaly the ORM of Django, cast the data returned by a query, so an IntegerField will be returned as a Int, and CharField as Unicode.

Which version of Django do you use ?

For the italian translation, it's a good idea, thank you. The Italian translation file is located here : emencia-django-newsletter/emencia/django/newsletter/locale/it So you have to edit the django.po file, compile it and relaunch your server.

For uploading your translation use this URL : http://www.transifex.net/projects/p/emencia-django-newsletter/c/master/

Contact me for further details.

Julien

martip commented 14 years ago

Hi Julien,

I forgot to mention that I'm using MySQL as db backend. I'm using Django version 1.1:

>>> django.VERSION
(1, 1, 1, 'final', 0)

Please, have a look at this shell trace (python manage.py shell):

import smtplib from emencia.django.newsletter.models import * s = SMTPServer.objects.all()[0] s.port 25L server = smtplib.SMTP(s.host, s.port) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/smtplib.py", line 239, in init (code, msg) = self.connect(host, port) File "/usr/lib/python2.6/smtplib.py", line 295, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket return socket.create_connection((port, host), timeout) File "/usr/lib/python2.6/socket.py", line 500, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): error: Int or String expected

explicitly converting to int, fixes the problem:

server = smtplib.SMTP(s.host, int(s.port)) server <smtplib.SMTP instance at 0x8757ecc>

then, for the login issue:

server.login (s.user, s.password) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/smtplib.py", line 574, in login (code, resp) = self.docmd(encode_cram_md5(resp, user, password)) File "/usr/lib/python2.6/smtplib.py", line 538, in encode_cram_md5 response = user + " " + hmac.HMAC(password, challenge).hexdigest() File "/usr/lib/python2.6/hmac.py", line 72, in init self.outer.update(key.translate(trans_5C)) TypeError: character mapping must return integer, None or unicode

explicitly encoding to utf-8, fixes the problem:

server.login (s.user.encode('utf-8'), s.password.encode('utf-8')) (235, 'go ahead')

I think that the port issue is related to MySQL and the login issue is related to the SMTP server I'm connecting to. But I'm just trying to guess.

As for the italian translation, I was already doing what you suggested but it was not working. My other models were properly translated so I compared my models with yours and I spotted the difference: in models.py you use ugettext while I use ugettext_lazy.

I found this in the Django documentation (http://docs.djangoproject.com/en/dev/topics/i18n/#lazy-translation): "Always use lazy translations in Django models. Field names and table names should be marked for translation (otherwise, they won't be translated in the admin interface)."

Replacing ugettext with ugettext_lazy in models.py fixed my issues.

Paolo

Fantomas42 commented 14 years ago

Effectively I wasn't using lazy translations... Thank your for your report, I have fixed it in the repository,

For the SMTP issues, in fact for the port, you get an Long. I don't know why, even for your Unicode problems...

Maybe I should make a test on MySQL because I have not this bug in SQLite and PostgreSQL. If you use SQLite, do you have the same problem ?

Julien

martip commented 14 years ago

Julien, it's exactly as we've supposed.

The port issue is related to MySQL; I switched to SQLite and everything works as expected:

import smtplib from emencia.django.newsletter.models import * s = SMTPServer.objects.all()[0] s.port 25 server = smtplib.SMTP(s.host, s.port) server <smtplib.SMTP instance at 0x970544c>

Anyway, I suggest to explicitly use the conversion to int, as it doesn't create problems if port is already an integer.

I also tried to use another SMTP server and I confirm that login issues are related to the server you're connecting to (the second server works without utf-8 encoding username and password). Again, to grant the maximum compatibility, I suggest to explicitly encode to utf-8... I tried to login to the second server forcing encoding and everything works fine.

Paolo

PS: I'm almost done with the italian translation ;-)

Fantomas42 commented 14 years ago

Unicode and Long are now fixed !