What is the process for sending an email using python. What I found in my research was either different than what I was attempting or didn't work when I attempted to actually use it. This doesn't seem like it would be a complicated task.
-
What are you using to send it? Google's api, Microsofts? – v0rtex Jan 17 '18 at 22:32
-
I am using python? – Hippolippo Jan 17 '18 at 22:35
-
What libraries are you importing for this? At the top of your code, when you write `import ...`, what is it that you're importing? Also - you need (i) to explain the exact problem you're having and any errors you get, and (ii) to attach an example of your code or this question is going to very quickly get down voted or closed. – Daniel R. Livingston Jan 17 '18 at 22:41
-
You need an SMTP server, yes. Gmail, for example is commonly used. – OneCricketeer Jan 17 '18 at 22:48
-
I need someone to teach me, I am not using anything right now, it was just I need to know so I can make a project in the future. – Hippolippo Jan 17 '18 at 22:50
-
Read this to get a little idea http://stackabuse.com/how-to-send-emails-with-gmail-using-python/ – Paandittya Jan 17 '18 at 22:56
-
1SO is not a coding service. If you don’t know how to program, I’d suggest google for free tutorials and watch YouTube videos. – Saleem Jan 18 '18 at 01:41
-
@Saleem I am not asking for you to code for me. I know how to code. I just wanted to know where to start in this whole pretty complicated process – Hippolippo Dec 19 '18 at 02:02
1 Answers
I made it in python3.6.3 and after that I rebuilt the code to python3.2.6 but it works. :)
First of all we gonna import few things which are in a standard Python library.
import smtplib
from getpass import getpass
from email.mime.text import MIMEText
Tip! If you want to send email from gmail you have to enable less-secure app: https://myaccount.google.com/lesssecureapps
We have to make an email so:
sender = 'example_sender@gmail.com'
receiver = 'example_receiver@gmail.com'
content = """The receiver will see this message.
Best regards"""
msg = MIMEText(content)
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = 'Simple app script'
Tip! Of course you can also read content for example from file by using:
with open('/path/to/your/file', 'r') as file: content = file.read()
Now we can handle a "magic" server side.
Tip! Server name you can easily found in your email settings. (IMAP/POP page) Here you have a list of servers I've found: https://www.arclab.com/en/kb/email/list-of-smtp-and-pop3-servers-mailserver-list.html
The solution for gmail, but you it can work with any server:
smtp_server_name = 'smtp.gmail.com'
#port = '465' # for secure messages
port = '587' # for normal messages
Tip! There are answers for difference of these ports: What is the difference between ports 465 and 587?
I think this sample of code is simply. By smtplib.SMTP_SSL we can only handle server by secure port (465). In other case we use different method.
if port == '465':
server = smtplib.SMTP_SSL('{}:{}'.format(smtp_server_name, port))
else :
server = smtplib.SMTP('{}:{}'.format(smtp_server_name, port))
server.starttls() # this is for secure reason
server.login(sender, getpass(prompt="Email Password: "))
server.send_message(msg)
server.quit()
When server is going to login you have to type your password after prompt in your shell.
And that's it. I ran this on Linux from command line.
Please feel free to ask for questions! :)
PS. I tested it on Windows 7 on fresh install of python 3.2.2. Everything works.

- 106
- 3
-
-
I got this: Traceback (most recent call last): File "C:/Python32/email.py", line 1, in
import smtplib File "C:\Python32\lib\smtplib.py", line 47, in – Hippolippo Jan 18 '18 at 21:11import email.utils File "C:/Python32\email.py", line 3, in from email.mime.text import MIMEText ImportError: No module named mime.text -
Yes it works. I tried on Windows 7, fresh install of python 3.2.2. And I got a message to my email. :) Try to reinstall python or simply install new version. – lszpiczakowski Jan 19 '18 at 09:28
-