Send mails with Python and an IMAP/SMTP Server

4.6k views Asked by At

i have a couple issues with sending Email with Python using my own SMTP/IMAP Server. Here's the code :

import sys
import imaplib
import smtplib
import email
import email.header
import datetime

smtp_session = smtplib.SMTP(mail_server)
try:
    smtp_session.ehlo()
except :
    err = sys.exc_info()[0]


message = """\
From: %s
To: %s
Subject: %s

%s""" % (email_from, ", ".join([email_to]), "subject", "body")
try:
    smtp_session.sendmail(email_from, [email_to], message)
    smtp_session.quit()
except:
    err = sys.exc_info()[0]

if err != "" or err !=None:
    NagiosCode = 2
    NagiosMsg = "CRITICAL: Script execution failed : " + str(err)

Ok so for the two issues i have: When i send a mail from my script i need the mail to appear in the "sent items" diretory of my mail box who send it.

Second issue i have : When sending my mail i catch this exception :

<class 'smtplib.SMTPException'>

EDIT : Exception trace :

  File "checkIMAP_client.py", line 153, in <module>
    smtp_session.login(login, password)
  File "/usr/lib64/python2.6/smtplib.py", line 559, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

EDIT : It seems my SMTP server doesn't require authentification. But the program still returns me an empty exception.

Code is updated.

1

There are 1 answers

0
Aurelien On BEST ANSWER

So for the two issues i had, i found the answers thanks to the above comments :

Putting my sent e-mail via SMTP in the right mailbox in the Sent directory : https://pymotw.com/2/imaplib/ Look for "Uploading messages"

Exception issue : I had no auth methods set on my SMTP server.