Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get server reply after sending a mail using smtplib SMTP.sendmail

Tags:

python

smtplib

I have a program to send mail using python smtplib. I have the mail sending part working fine, but I also need to capture the server return message after a mail has been sent. For example postfix returns the following message after a mail has been queueed:

reply: '250 2.0.0 Ok: queued as EB83821273B\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as EB83821273B
data: (250, '2.0.0 Ok: queued as EB83821273B')

What I am really interested is the error code (250) and the queue id (EB83821273B). I can print these if I set set_debuglevel(1), but I need to capture this in a variable for further logging and processing.

thanks and regards,

raj

like image 879
Rajkumar S Avatar asked Sep 29 '10 10:09

Rajkumar S


People also ask

What does Smtplib SMTP do?

The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions).

What is Smtplib in Python How can you use Python built in mail server explain?

The smtplib module The smtplib is a Python library for sending emails using the Simple Mail Transfer Protocol (SMTP). The smtplib is a built-in module; we do not need to install it. It abstracts away all the complexities of SMTP.


1 Answers

If you are using the sendmail method on an SMTP instance, then it will return

a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.

if you use the docmd method on the same class, the it will return

a 2-tuple composed of a numeric response code and the actual response line (multiline responses are joined into one long line.)

like image 116
aaronasterling Avatar answered Oct 08 '22 22:10

aaronasterling