how to send email from droplet

I tried to follow the steps in
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 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'] = ""
    mail_obj['To'] = ''
    if attmnt_file_loc!=None:
        with open(attmnt_file_loc, 'rb') as fp:
            file_data =
            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('', 587) 
    s.login("", "mygmailpassword")

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.

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.

Hope that this helps!

Hi @bodoia,

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

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.