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.
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!
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:
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!
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
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.