Ben
Ben

Reputation: 31

Send email via Python/django on 1&1

I am developing a website with django framework. And I recently tried to release it on a 1&1 shared hosting. I managed to make the project run well, except for one last “detail”: I can't send email from django.

I tried almost everything in settings (different emails, ports, etc.), but each time I got a beautiful '500 Internal Server Error' =/ (whereas it went fine on a free alwaysdata server)

In order to find the origin of this issue, I tried different things:

to test send_mail and EmailMessage via the python interpreter:

>>> send_mail('a subject', 'a test message', '[email protected]', ['[email protected]'])
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: a subject
From: [email protected]
To: [email protected]
Date: Tue, 14 Jan 2014 22:12:48 -0000
Message-ID: <[email protected]>

a test message
-------------------------------------------------------------------------------
1

But... I got nothing in my inbox. My settings being:

EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS       = True
EMAIL_HOST          = 'smtp.gmail.com'
DEFAULT_FROM_EMAIL  = '[email protected]'
EMAIL_HOST_USER     = '[email protected]'
EMAIL_PORT          = 587

to connect to an SMTP server via the python interpreter:

./manage.py shell
>>> from smtplib import SMTP
>>> smtp_conn = SMTP()
>>> smtp_conn.connect('smtp.gmail.com', 25)

And nothing happens (I tried it with auth.smtp.1and1.fr, or port 587...); when I interrupt the process, it tells me:

^CTraceback (most recent call last):
  File "<console>", line 1, in <module>
  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 554, in create_connection
    sock.connect(sa)
  File "<string>", line 1, in connect
KeyboardInterrupt

to test with a php file:

With a basic:

<?php
  if (mail("[email protected]", "a subject", "a test")) {
    echo("<p>Email successfully sent!</p>");
  }
  else {
    echo("<p>Email delivery failed…</p>");
  }
?>

And the mail was sent! So, it seems that without SMTP connection, it is possible (at least, via php).

Conclusion

So, my questions are: do you think I can manage to correct this bug, and otherwise is it possible to send mail without smtp connection ("like php")?

Upvotes: 3

Views: 1875

Answers (1)

Mike S
Mike S

Reputation: 1577

1and1 does not allow SMTP within 1and1. To send an email when running on 1and1...

#!/usr/bin/python
import os
mail_to = "[email protected]"
mail_from = "[email protected]"
subject = "test message"
header = """From: {0}
To: {1}
Subject: {2}
""".format(mail_from, mail_to, subject)
msg = header + "a test from me"
sendmail = os.popen("/usr/lib/sendmail -t", "w")
sendmail.write(msg)
sendmail.close()

The blog post linked shows an example that sends mail using SMTP through 1and1 via an external machine. Perhaps you can override Django's send_mail (or maybe this gives you [or someone else] a hint as to why it doesn't work.) This should allow you to send email in the Python interpreter, anyway.

Source: https://flenniken.net/blog/send-email-using-python-and-1and1/

Upvotes: 1

Related Questions