4

I am not at all familiar with SMTP but I am working on sending emails through Python code. I have the code but I need to pass SMTP host name for it to actually work. Is there any service which provides a free SMTP service that I leverage for testing out my code? I looked around to create my own SMTP server but couldn't find something that provides a step by step guide to create a SMTP server. I want to create a free server(or if there is any free service) that will provide me with a host name(ip address) so that I can put that host name in my python code and execute it from any machine.

If anyone can point me in the right direction it will be helpful.

user2916886
  • 847
  • 2
  • 16
  • 35

2 Answers2

4
import smtplib

username = 'user'
password = 'pwd'

from_addr = 'username@gmail.com'
to_addrs = 'username@gmail.com'

msg = "\r\n".join([
  "From: username@gmail.com",
  "To: username@gmail.com",
  "Subject: subject",
  "",
  "message"
  ])

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg)
server.quit()

You can use mutt linux command also here.

See :
https://docs.python.org/3/library/smtplib.html
https://support.google.com/a/answer/176600?hl=en

glegoux
  • 3,505
  • 15
  • 32
  • is `smtp.gmail.com:587` a valid SMTP host name that I can actually use in my code or is just a sample example? – user2916886 Jun 21 '17 at 22:45
  • @user2916886 check the last link. It is mentioned there. I think you need a gmail account or similar. It's best you read it yourself. – bbastu Jun 21 '17 at 22:49
  • I am getting `OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions` when I am trying to run my code using `'smtp.gmail.com:587` – user2916886 Jun 22 '17 at 14:29
  • I was able to resolve above error by disabling email sending blocking in my antivirus as well as enabling `access from less secure app` in my gmail account – user2916886 Jun 22 '17 at 15:09
  • the answer although a working one, does not respond whatsoever to the asked question which is "how to create a free SMTP server". the asnwer responds more to the question "how to use an existing GMAIL account as SMTP server to send mails using python". i think you answer can be found in https://stackoverflow.com/questions/2690965/a-simple-smtp-server-in-python – Kostas Markakis Nov 16 '21 at 13:47
2

You need service like https://mailtrap.io/. You'll get SMTP server address (eventually port number) that you point your application to. All e-mails produced by your application will be then intercepted by mailtrap (thus not delivered to the real To: address).

They offer free variant that seems to be suitable for your needs.