Question

i am not able to send email using django in digital ocean

  • Posted on February 1, 2022
  • CuteLapisAnemoneAsked by info

My Code is working fine locally but not on digital ocean…What should I do. Do I have to install anything on droplet for sending mail. I am stuck. Please help me.


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I had a similar problem, maybe this helps. It turned out that the message format made the difference. smtp_server.sendmail would send plain strings but not anything beyond that. Using EmailMessage from email.message resolved the issue. Maybe give it a try. Here the code (with the send_mail commented out for reference, this worked locally but not on DO):

#from django.core.mail import send_mail
from xxx.settings import EMAIL_BACKEND, DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_PORT, EMAIL_USE_TLS, EMAIL_USE_SSL
import smtplib
from email.message import EmailMessage

def send_otp_to_user_email(email, otp):  
    subject = 'PW change'
    message_text = 'Please use thos code to reset your PW: ' + str(otp)
    email_from = DEFAULT_FROM_EMAIL
    recipient_list = [email,]
    
    '''result = send_mail(
        subject,
        message_text,
        email_from,
        recipient_list,
        auth_user = EMAIL_HOST_USER,
        auth_password = EMAIL_HOST_PASSWORD,
        fail_silently=False)'''
        
    msg = EmailMessage()
    msg['Subject'] = subject
    msg['From'] = email_from
    msg['To'] = email
    msg.add_header('Content-Type','text/html')
    msg.set_content(message_text)

    try:
        #smtp_server = smtplib.SMTP_SSL(EMAIL_HOST, EMAIL_PORT)
        smtp_server = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
        smtp_server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
        try:
            smtp_server.sendmail(email_from, recipient_list, msg.as_string())
        except Exception as ex_sendmail:
            smtp_server.close()
            return -1
        smtp_server.close()
        return 1
    except Exception as ex:
        return 0
    
    return result
Bobby Iliev
Site Moderator
Site Moderator badge
February 2, 2022

Hi there,

In addition to what KFSys already mentioned, what I personally do is to use an SMTP service like SendGrid for example for my outgoing emails.

That way I do not have to manage my own email service on my Droplets and most SMTP providers like SendGrid do offer a free plan which is sufficient for a lot of use cases.

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
February 2, 2022

Hey @cutelapisanemone,

Which port are you using to send out e-mails, is it port 25 or 465? If you are using port 25, I’ll recommend using 465 as it’s considered more secure. If however you want to use 25 or you are still having issues with port 465, I’ll recommend contacting DigitalOcean’s Support here:

https://www.digitalocean.com/support/

DigitalOcean blocks traffic on port 25 for new accounts in an effort to prevent spam from leaving their Droplets and worsening the reputation of their IPs. As such contacting them should be enough to have the block on the IP removed, if that’s the case!