Search code examples
pythonsmtpsmtplib

How to get smtp email log


I want to get a log when I email to the wrong email address.

so, I wrote this command.

mailserver = mymailserver
to_email = emailaddress
from_email = fromaddress
subject = SBJECT
original_message = TEXT
message = MESSAGE

server = smtplib.SMTP(mailserver)
debug = server.set_debuglevel(1)
server.sendmail(from_mail,to_mail, message)
server.quit()
print(debug)

I just want to know connection status log.

wchich code should I edit?

I tried this scripts, but it does not work well.

server = smtplib.SMTP(mailserver)
mail_response = server.sendmail(from_mail,to_mail, message)
server.quit()
print(mail_response)

Thank you for helping me.


Solution

  • Try This and read the docs. Hope it will help you

    # Import smtplib for the actual sending function
    import smtplib
    
    # Import the email modules we'll need
    from email.message import EmailMessage
    
    # Open the plain text file whose name is in textfile for reading.
    # Or simply skip this part
    with open(textfile) as fp:
        # Create a text/plain message
        msg = EmailMessage()
        msg.set_content(fp.read())
    
    # me == the sender's email address
    # you == the recipient's email address
    msg['Subject'] = f'The contents of {textfile}'
    msg['From'] = me
    msg['To'] = you
    
    # Send the message via our own SMTP server.
    s = smtplib.SMTP('localhost')
    s.send_message(msg)
    s.quit()