SMTP Authenticated email using Python script

SMTP Authenticated email using Python script


System administrators generally use the sendmail or mail utility from mailx to send emails. Which automatically send the email from username@hostname. But there are times when you want to send email from external email server and it requires SMTP authentication. Python is a very powerful scripting language and we can use this below simple script send SMTP authenticated emails.


import smtplib

email_to = ''
username = ''
password = 'password of'

smtpserver = smtplib.SMTP("",25)
header = 'To:' + email_to + '\n' + 'From: ' + username + '\n' + 'Subject: Python SMTP Auth\n'
msg = header + '\n\n This is a test message generated from python script \n\n'
smtpserver.sendmail(username, email_to, msg)
print 'Email sent successfully'