Question

how to send email from droplet

I tried to follow the steps in https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-14-04
but when I am in my DigOcean droplet and I try to pip install postfix I get ERROR: Conld not find a version that satisfies the requirement postfix (from versions: none).
I also tried to follow the steps in https://www.rosehosting.com/blog/how-to-setup-mail-server-with-virtual-domains-using-postfix-dovecot-and-opendkim/ and I entered command ufw allow 587 with response: Rules updated Rules updated (v6), but when I enter ufw app list all I get is Available applications: OpenSSH I have already installed mailutils the code I’m trying to run is this (which works fine from my local laptop, just not from code running on my DO droplet). What I’m trying to achieve is sending updates to myself in my email to keep abreast of what’s happening in my droplet program.

import smtplib
import mimetypes
from email.message import EmailMessage

def send_mail(body_msg, attmnt_file_loc=None):
    mail_obj = EmailMessage()
    mail_obj['Subject'] = 'This email contains an attachment'
    mail_obj['From'] = "mygmail@gmail.com"
    mail_obj['To'] = 'myemail@msn.com'
    mail_obj.set_content(body_msg)
    
    if attmnt_file_loc!=None:
        with open(attmnt_file_loc, 'rb') as fp:
            file_data = fp.read()
            maintype, _, subtype = (mimetypes.guess_type(attmnt_file_loc)[0] or 'application/octet-stream').partition("/")    
        mail_obj.add_attachment(file_data, maintype=maintype, subtype=subtype, filename=attmnt_file_loc)
        
    s = smtplib.SMTP('smtp.gmail.com', 587) 
    s.starttls()
    s.login("mygmail@gmail.com", "mygmailpassword")
    s.send_message(mail_obj)
    s.quit()

Submit an answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

What I could suggest here is using an SMTP plugin for your WordPress site.

That way you will be able to specify an SMTP server with authentication which your emails would be going through. You could for example use SendGrid’s SMTP service. This would drastically increase the delivery rate of your emails as well compared to using plain PHP mail.

I’ve been using this plugin for a while now and it works very well: Easy WP SMTP.

https://www.digitalocean.com/community/tutorials/how-to-get-wordpress-email-notifications-on-a-digitalocean-droplet

Hope that this helps!

Hi @bodoia,

First, I’ll recommend you to check this tutorial rather than the one you mentioned:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-20-04

The one mentioned is heavily out of date.

Additionally, I don’t think you need to install postfix with pip. You just need to have it installed on your Droplet with:

sudo apt update
sudo apt install mailutils

Mailutils is a package, which bundles Postfix with a few supplementary programs that you’ll use to send emails.

Now, looking at your Python script, you have the following libraries:

import smtplib
import mimetypes
from email.message import EmailMessage

I think all of them come by default installed with Python but if they don’t those are the ones you need to use ```pip install`` on.