[Python] How to send email from DigitalOcean droplet in python3 using local mail server to a...

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to send email from a DigitalOcean droplet (Ubuntu 16.04 32 bit) in python3.5 using the local mail/smtp server, primarily to a gmail account. However, currently no errors are displaying from my python script or the server command, but the emails are not showing up in my gmail inbox or spam folder.

    I have also followed similar steps from my laptop where possible, with no results.

    I want to use the local mail/smtp server to send emails to avoid storing authentication details in the server, and since it will not be receiving emails then I am not as worried about security as if receiving (e.g. not worried about injection attack).

    To start off, I run the command;

    python3 -m smtpd -n -c PureProxy localhost:1025 &


    I run the following script. My understanding is that this should send the email to the local mail/smtp server, which should then handle ensuring it's delivery.

    import smtplib
    from email.mime.text import MIMEText

    msg = MIMEText('test message from droplet body')
    msg['Subject'] = 'test message from droplet subject'
    msg['From'] = 'droplet@mydomain.com.au'
    msg['To'] = 'mygmailusername@gmail.com'

    s = smtplib.SMTP('localhost', 1025)
    s.set_debuglevel(True)
    s.send_message(msg)
    s.quit()


    With the above method, no error messages occur. However the emails are never received. All methods I have tried so far have had this result.

    Using the DebuggingServer instead of PureProxy and the same steps as before provides output similar to below;

    ---------- MESSAGE FOLLOWS ----------
    Content-Type: text/plain; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Subject: test message from droplet subject
    From: droplet@mydomain.com.au
    To: mygmailusername@gmail.com
    X-Peer: 127.0.0.1

    test message from droplet body
    ------------ END MESSAGE ------------


    I have tried the first example on the following page

    https://docs.python.org/3/library/email-examples.html

    However this fails due to a refused connection error.

    I have also tried to check the spamminess of my domain's emails using the following site, however it times out - this indicates to me that it may be a problem with DigitalOcean allowing emails through it's firewall.

    https://www.mail-tester.com/

    I have checked reputation of my domain using this site - it says it is neutral (using talosintelligence link in comments)

    I have tried sending email using the prior instructions to my namecheap private email, but without success.

    One possibility I read about is that a PTR DNS record needs to be set, to allow reverse DNS lookup. I am not sure what to do with this though, and I would have to override the default droplet name.

    Extra details

    I am using a .com.au domain registered with namecheap. I am also using a private email service with namecheap, which required setting multiple DNS records including MX - I added all on the following link, including the optional ones, on my digitalocean account (link in comments)

    I also set up the digitalocean nameservers on the namecheap account (link in comments)

    I can send and receive email on the private email service.

    Continue reading...

Compartilhe esta Página