Send Email to locally running SMTP server

Prerequisites

  • Must have a smtp server running locally
  • Must have python installed

Steps

To do this we are going to be using a simple python script

So first open up a text editor

Add the following code snippet

#!/usr/bin/python

import smtplib
from smtplib import SMTPException


sender = 'FROM_DOMAIN'
receivers = ['EMAIL_BOX']

message = """From: From Person <FROM_DOMAIN>
To: To Person <TO_DOMAIN>
Subject: SUBJECT

BODY
"""

try:
 smtpObj = smtplib.SMTP('localhost', PORT)
 smtpObj.sendmail(sender, receivers, message)
 print "Successfully sent email"
except SMTPException:
 print "Error: unable to send email"

replace the following variables with their correct values:

  • FROM_DOMAIN – Can be anything. Ex: from@fromdomain.com
  • TO_DOMAIN -Can be anything. Ex: to@todomain.com
  • EMAIL_BOX – The location you are sending it
  • SUBJECT – the subject of the email
  • BODY – The body of the email
  • PORT – What port your smtp server is running on

Then just make sure the file you save it in is executable

To do this from the command line it is simply

chmod +x <NAME_OF_FILE>

then execute it

./<NAME_OF_FILE>

 

 

Advertisements