Send emails in a django development environment on OS X leopard - django

Hay, i was wondering how i would go about setting up django so that i can send emails from a standard Leopard installation.
In php i just use mail() and it sends the mail for me.
Thanks

From the docs:
Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS setting controls whether a secure connection is used.
So set your EMAIL_HOST to a friendly SMTP server which will relay mail for you, and away you go.
Again, from the docs:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)

Related

Django Sendgrid Heroku setup

I use a Django / Sendgrid / Heroku setup which was working well but it stopped with no apparent reason.
my django settings:
EMAIL_HOST="smtp.sendgrid.net"
EMAIL_HOST_PASSWORD="..."
EMAIL_HOST_USER="..."
EMAIL_PORT=587
EMAIL_USE_TLS=True
with this settings, if I send simple emails with the django library...
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
from_email_address,
[to_email_address],
fail_silently=False,
)
I get this error
--> SMTPServerDisconnected: Connection unexpectedly closed
I can still send emails using the sendgrid python library
import sendgrid
sg = sendgrid.SendGridAPIClient(apikey)
sg.send(data)
Is there anything I am missing?
Just figured this out:
Sendgrid dynamically provides a password through the SENDGRID_PASSWORD Heroku environment variable. However the variable used for the email settings is EMAIL_HOST_PASSWORD which was not updated after our password changed.
Be sure that both variables are identical

How to Send an Email Django

Previously, I was using SendGrid to serve emails using Django's SMTP backend, which worked perfectly fine. However, now I would like my project to use Microsoft Exchange. When I updated my SMTP configuration in settings.py, upon the submission of some form to be emailed, the page timesout when trying to reach the server: TimeoutError: [Errno 60] Operation timed out.
settings.py
# E-Mail
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.microsoft365.com'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
views.py
# Send email
send_mail('!!New Mail!! ', content, 'noreply#domain.com', ['username#domain.com'], fail_silently=False)
I solved my problem when I used the correct SMTP port (25, 465, 587) for my particular mail server.
settings.py
EMAIL_PORT = 25

send_mail works locally but not on production hosting

The following works locally but as I deployed it to production hosting at Digital Ocean, the email is not sending as I test on shell command (python manage.py shell) like below. The send_mail line just got stuck there and am getting error: [Errno 101] Network is unreachable after few minutes. How can I capture the error on the email sending? Please advise how can I troubleshoot this issue.
from django.core.mail import send_mail
send_mail('test email', 'hello world', 'xxxx#gmail.com', ['xxxx#gmail.com'],fail_silently=False)
# Email settings
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx#gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Your Django app settings look correct but you should also head over to https://accounts.google.com/DisplayUnlockCaptcha and make sure you enable access to lower security applications.
Also consider creating an application-specific password if you're using Two-Factor-Authentication.

Why SMTP sendmail works fast in local but very slow in my dev server Django 1.6

I am using SMTP sendmail in my project and tried to send a mail using SMTP sendmail. Its works fine and sends mail in 3-4 seconds. But the same code takes around 5 Minutes in dev server. Can you please help me.
In my view function
def send_email(subject, message, recipients, contenttype, attachments = []):
try:
from_email = "gauravnagpal2002#gmail.com"
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ",".join(recipients)
msg.attach( MIMEText(message) )
for f in attachments:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
server = smtplib.SMTP('localhost')
server.sendmail(from_email, recipients, msg.as_string())
server.quit()
except Exception, e:
logger.error(str(e))
Can any one helps me to figure it out whats am doing wrong?
why don't you use django inbuilt email,
Edit settings.py with code below:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail#gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
Run interactive mode, python manage.py shell
Import the EmailMessage module,
from django.core.mail import EmailMessage
# Send the email,
email = EmailMessage('Subject', 'Body', t=['mickeyckm#mangooranges.com'])
email.save()
My guess is this probably isn't a django problem (or a python problem, because you're not using django to actually send your e-mail). I've had a similar problem before by not including my server's fully-qualified name in the hosts file.
Open /etc/hosts and make sure that your fully-qualified domain name of your server maps to the loopback address (127.0.0.1).
If this is your only web server (something like this)
127.0.0.1 localhost your.servers.fully.qualified.domain.com
Really, though.. We need more information to help you. If you try this solution (make sure to restart the sendmail daemon) and you're still having problems, open up the log for sendmail (if you're on ubuntu, it's going to be somewhere like /var/log/mail.log) and take a look at the log entries for when sendmail is called, then update your question with the relevant log entries.
See also:
https://superuser.com/questions/626205/sendmail-very-slow-etc-hosts-configuration/626219#626219
https://www.digitalocean.com/community/questions/sendmail-is-slow-to-send-mail

Sending email from Django Mezzanine running on Heroku

I am using rackspace email and having problem sending email via Mezzanine(1.4.10) form page.
This is my settings:
EMAIL_HOST = 'secure.emailsrvr.com'
EMAIL_PORT = 465 # other ports also tried
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'info#example.com'
EMAIL_HOST_PASSWORD = 'secret'
This is Rackspace documentation:
http://www.rackspace.com/apps/support/portal/1088
But I can send email from the console like this:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)
It return 1 and I actually got the email. If I do that from Mezzanine, I assumed it also return 1 according the redirect URL after the email was sent, but I don't get the email.
In addition to the settings mentioned in the Django documentation, try setting DEFAULT_FROM_EMAIL. You will probably want to use the same value as EMAIL_HOST_USER.