I'm trying to send an email within python, but the program is crashing when I run it either as a function in a larger program or on it's own in the interpreter.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "exampleg#gmail.com"
toaddr = "recipient#address.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Hi there"
body = "example"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
In the interpreter, it seems to fail with server = smtplib.SMTP('smtp.gmail.com', 587)
Any ideas?
My standard suggestion (as I'm the developer of it) is yagmail.
Install: pip install yagmail
Then:
import yagmail
yag = yagmail.SMTP(fromaddr, "pasword")
yag.send(toaddr, "Hi there", "example")
A lot of things can be made easier using the package, such as HTML email, adding attachments and avoiding having to write passwords in your script.
For the instructions to all of this (and more, sorry for the cliches), have a look at the readme on github.
That's because you are getting error when trying to connect Google SMTP server.
Note that if you are using Google SMTP you should use:
Username: Your gmail address
Password: Your gmail password
And you should be already logged in. If you still get an error, you should check what is the problem in this list: https://stackoverflow.com/a/25238515/1600523
Note: You can also use your own SMTP server.
Related
I have a code which was working like a half year ago. It basiclly sends email.
import smtplib
import socket
gmail_user="SENDERMAIL"
gmail_password="SENDERPASS"
to = 'SENDTOTHIS'
email_text = "ADSADSADSA"
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.starttls()
server.sendmail(gmail_user, to, email_text)
server.close()
#I was using this code below and it was working. I tried above code but it also did not work.
#server = smtplib.SMTP("smtp.gmail.com:587")
#server.ehlo()
#server.starttls()
#server.ehlo()
#server.login(gmail_user, gmail_password)
#server.sendmail(gmail_user, to, email_text)
#server.close()
print("Done")
except Exception as exception:
print(exception)
Here's exception
(534, b'5.7.14
5.7.14 KL7_2qGSLW9IBjP8dKKgP67bEgyKNc5ls76dnVDZcUlVQjJUQb0JX9BIVi_Agb84vKNOKB
5.7.14 fshB0ngZ_Tn8ocDpDHKavRKXmluVjHo5YM7ADKENtWn4aVTxyvaBlbXRGpA1EBh91bdV-o
5.7.14 pwiAWUHXKmRQEuSNSiFcv68DP4a7ghIu9YKnTyqtUEhGd4HgKtxa4Jz0mhSQDjD13UQWYB
5.7.14 -YEL5Sd2h5YxN8kkSAsK-J_hXMbpy7wNyeCov8lq1Aa3spZzgo> Please log in via
5.7.14 your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 f132-v6sm3660398wme.24 - gsmtp')
I did try to
logined gmail
add device to trusted devices
turned on IMAP via gmail
let less secure apps
tried this:
https://support.google.com/mail/answer/7126229?visit_id=636711453029417344-336837064&rd=2#cantsignin
There are to many ways to solve this problem. I hope this code helps.
The only thing you need to do is filling the required variables.
import socket
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#
message = "Your message" # Type your message
msg = MIMEMultipart()
password = "********" # Type your password
msg['From'] = "from#gmail.com" # Type your own gmail address
msg['To'] = "To#gmail.com" # Type your friend's mail address
msg['Subject'] = "title" # Type the subject of your message
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
I can also advise to use a simpler library (a wrapper on top of smtplib, to make sure there are no other factors involved).... like yagmail (disclaimer: I'm the developer).
Try to see if this works:
import yagmail
yag = yagmail.SMTP("username", "password")
yag.send(subject="hi")
I am trying to send an email using GMAIL with a subject and a message. I have succeeded in sending an email using GMAIL without the implementation of subject and have also been able to receieve the email. However whenever I try to add a subject the program simply does not work.
import smtplib
fromx = 'email#gmail.com'
to = 'email1#gmail.com'
subject = 'subject' #Line that causes trouble
msg = 'example'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login('email#gmail.com', 'password')
server.sendmail(fromx, to, subject , msg) #'subject'Causes trouble
server.quit()
error line:
server.sendmail(fromx, to, subject , msg) #'subject'Causes trouble
The call to smtplib.SMTP.sendmail() does not take a subject parameter. See the doc for instructions on how to call it.
Subject lines, along with all other headers, are included as part of the message in a format called RFC822 format, after the now-obsolete document that originally defined the format. Make your message conform to that format, like so:
import smtplib
fromx = 'xxx#gmail.com'
to = 'xxx#gmail.com'
subject = 'subject' #Line that causes trouble
msg = 'Subject:{}\n\nexample'.format(subject)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login('xxx#gmail.com', 'xxx')
server.sendmail(fromx, to, msg)
server.quit()
Of course, the easier way to conform your message to all appropriate standards is to use the Python email.message standard library, like so:
import smtplib
from email.mime.text import MIMEText
fromx = 'xxx#gmail.com'
to = 'xxx#gmail.com'
msg = MIMEText('example')
msg['Subject'] = 'subject'
msg['From'] = fromx
msg['To'] = to
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login('xxx#gmail.com', 'xxx')
server.sendmail(fromx, to, msg.as_string())
server.quit()
Other examples are also available.
Or just use a package like yagmail. Disclaimer: I'm the maintainer.
import yagmail
yag = yagmail.SMTP("email.gmail.com", "password")
yag.send("email1.gmail.com", "subject", "contents")
Install with pip install yagmail
I'm writing a python script to send emails from the terminal. In the mail which I currently send, it is without a subject. How do we add a subject to this email?
My current code:
import smtplib
msg = """From: hello#hello.com
To: hi#hi.com\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me#me.com', ['anyone#anyone.com '], msg)
server.quit()
You need to put the subject in the header of the message.
Example -
import smtplib
msg = """From: hello#hello.com
To: hi#hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me#me.com', ['anyone#anyone.com '], msg)
server.quit()
You can simply use MIMEMultipart()
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
msg = MIMEMultipart()
msg['From'] = 'EMAIL_USER'
msg['To'] = 'EMAIL_TO_SEND'
msg['Subject'] = 'SUBJECT'
body = 'YOUR TEXT'
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('email','password')
server.sendmail(email_user,email_send,text)
server.quit()
Hope this works!!!
import smtp
def send_email(SENDER_EMAIL,PASSWORD,RECEIVER_MAIL,SUBJECT,MESSAGE):
try:
server = smtplib.SMTP("smtp.gmail.com",587)
#specify server and port as per your requirement
server.starttls()
server.login(SENDER_EMAIL,PASSWORD)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (SENDER_EMAIL, ", ".join(TO), SUBJECT, MESSAGE)
server.sendmail(SENDER_EMAIL,TO,message)
server.quit()
print 'successfully sent the mail'
except:
print "failed to send mail"
send_email("sender#gmail.com","Password","receiver#gmail.com","SUBJECT","MESSAGE")
Indeed, the subject you missed. Those things can easily be avoided by using some API (such as yagmail) rather than the headers style.
I believe yagmail (disclaimer: I'm the maintainer) can be of great help to you, since it provides an easy API.
import yagmail
yag = yagmail.SMTP('hello#gmail.com', 'yourpassword')
yag.send(to = 'hi#hi.com', subject ='Your subject', contents = 'Some message text')
That's only three lines indeed.
Install with pip install yagmail or pip3 install yagmail for Python 3.
More info at github.
I was experimenting with an email python script and was wondering if when writing a python-based email script is it is less secure as opposed to when credentials are send over the internet when logging into a web page? In the following script, are the user and pass in the clear?
import smtplib
from email.mime.text import MIMEText
GMAIL_LOGIN = 'xxxxxx#gmail.com'
GMAIL_PASSWORD = 'amiexposed?'
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=GMAIL_LOGIN):
msg = MIMEText(message)
msg['Subject'] = 'Test message'
msg['From'] = from_addr
msg['To'] = to_addr
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
server.sendmail(from_addr, to_addr, msg.as_string())
server.close()
if __name__ == '__main__':
send_email('testing email script', 'This is a test message')
That would entirely depend how the TLS connection is set up. If you are requiring valid certificates (I believe if a certificate which is not trusted is encountered, your startTLS method will throw an exception (I'm not sure you should verify this)). But considering you are setting up TLS, and sending everything over the TLS connection, everything should be encrypted. This means neither your password, username or even your message and addressees will be sent in plain text.
So no, your username and password are not send clear.
I have a React website up and running and I need to implement a mailer - it's for a wedding website and people would fill up the confirmation form and it'll automatically send e-mail to the organizers.
I've did it before using Ajax + PHP but I've wanted to learn Python and thought this would be a great opportunity.
Ok, so I do have a Python script that correctly sends an e-mail when I run it on my machine:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "from#email"
toaddr = "to#email"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Subject"
body = "Message"
msg.attach(MIMEText(body, 'plain', 'utf-8'))
server = smtplib.SMTP('smtp server', port)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Now I don't know how to proceed with the next steps:
Call Python script (passing parameters) from my React App;
Host Python script on my web server (Dreamhost).
I've already searched about Flask but I'm not sure if this is what I need.
Could someone please enlighten me?
Thanks!