1

I can sent emails from a perl script but not from a python script.

I've created a perl script that runs on a daily basis via jenkins and if some checks fails it sends me an email.

I'm trying to migrate the script from perl to python3.7 but can't send an email from python.

This is the code in perl:

use strict;
use warnings;

use Email::MIME;
use Email::Sender::Simple qw(sendmail);
use Data::Dumper;

my $message = Email::MIME->create(
    header_str => [
    From    => 'my_email@domain.com',
    To      =>  $email,
    Subject => 'just a subject',
    ],
    attributes => {
        encoding => 'quoted-printable',
        charset  => 'ISO-8859-1',
    },
    body_str => $arr_msg,
    );

    my $email_test = sendmail($message);

}

This is the code in python:

import smtplib

server = smtplib.SMTP('127.0.0.1', 25) # also tried my-domain.com with port 2525

server.sendmail(
    "me@my-domain.com", 
    "someone@address.com", 
    "this message is from python")
server.quit()

It dies at server = smtplib.SMTP('127.0.0.1', 25) with error:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Output of lsof -i :25

COMMAND  PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
master  2728 root   12u  IPv4 1774572613      0t0  TCP 
localhost.localdomain:smtp (LISTEN)

What could it be?

rooger
  • 135
  • 3
  • 14
  • Maybe this might help https://stackoverflow.com/a/46521290/4180176 – Joshua Nixon Jan 29 '19 at 18:14
  • If I'm using smtplib.SMTP_SSL() I'm getting Connection reset by peer – rooger Jan 29 '19 at 18:23
  • So the email domain you are attempting to connect to is local? Perhaps try ```localhost``` or ```0.0.0.0```, is the port correct? I saw an example where the port is not provided at call, try that too. Throwing ideas out. – Joshua Nixon Jan 29 '19 at 18:27
  • Yes, I'm every host/port I'm trying I get Connection unexpectedly closed unless I use 127.0.0.1 with port 25 and with SMTP_SSL() then I get Connection reset by peer – rooger Jan 29 '19 at 18:29
  • Your error occurred when I attempted to connect to gmail with an incorrect port. Did you try omitting the port parameter when you call ```.SMTP```? – Joshua Nixon Jan 29 '19 at 18:40
  • This can be achieved through Jenkins post build action as well, is there any limitation that is stopping you from using Jenkins feature of extended email? – Surendra Deshpande Jan 29 '19 at 19:32
  • @SurendraDeshpande Nothing is stopping me from using that jenkins feature, just for learning reasons I'm trying to achieve this via Python – rooger Jan 30 '19 at 16:30

1 Answers1

0

Yes i have done it for an gmx-mail

import smtplib
from email.mime.text import MIMEText


class MailService:
    senderEmail = "..."
    fromMail = "..."

    def __init__(self):
        self.server = smtplib.SMTP('mail.gmx.net', 587)  # Die Server Daten
        self.server.starttls()
        self.server.login(self.senderEmail, "passwort")  # Das Passwort

    def __delete__(self, instance):
        self.server.quit()

    def sendMail(self, toMail, text, subject):
        msg = MIMEText(text)
        msg['From'] = self.fromMail
        msg['To'] = toMail
        msg['Subject'] = subject
        mail = msg.as_string()
        self.server.sendmail(self.fromMail, toMail, mail)
  • Hmm this wont work, I'm not trying to connect to an external mail server, I'm trying to make it work with my mail server that is installed on the server – rooger Jan 29 '19 at 18:23