iTayb
iTayb

Reputation: 12753

Start local SMTP server

I'm going to transfer crash dumps from clients to me through the mail mechanism. Therefore, I can't use any public SMTP servers, as packing any account's credentials with the application is unacceptable.

Therefore, I need to send mails through my application directly to the destination mail server.

How can I achieve this in python? (I'm using windows so sendmail is not an option)

Upvotes: 0

Views: 1797

Answers (1)

abarnert
abarnert

Reputation: 365975

Just use smtplib in the standard library.

Trying to write code that can send mail to anyone is problematic, because smtplib connects to servers client-to-server style rather than server-to-server-relay style.

But if you only need to send mail to one particular server, which you control, it's trivial. Just configure your server at 'mail.example.com' to accept any mail from '[email protected]' to '[email protected]'.

Your code will look something like this:

import smtplib
addr = '[email protected]'
def send_crash_report(crash_report):
    msg = ('From: {}\r\nTo: {}\r\n\r\n{}'.format(
           addr, addr, crash_report)
    server = smtplib.SMTP('mail.example.com')
    server.sendmail(addr, [addr], msg)
    server.quit()

As a side note, if you're just starting on a crash report collector, you may want to consider using a web service instead of a mail address. You're going to run into problems with people who can't access port 25 through their corporate firewall/proxy, write code that extracts the crash reports from an inbox (and/or searches via IMAP or mbox or whatever), deal with spammers who somehow find [email protected] and flood it with 900 messages about Cialis for each actual crash report, etc.

Upvotes: 2

Related Questions