I used server.sendmail(fromaddr, toaddrs, msg) to send automated emails using python.
How can I add an email subject to the mail?
I tried to look in SMTP documentation and didn't find any documentation for that.
From How to send an email with Gmail as provider using Python? :
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
Related
What am I doing wrong? Should be so simple.After I execute - nothing happens, no errors, no email. Nothing.
I am using Jupyter.
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = 'email#email.com'
gmail_pwd = 'pass'
FROM = 'email#email.com'
TO = 'email#email.com'
SUBJECT = 'Test'
TEXT = 'Hello, this is test email'
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print ('successfully sent the mail')
except:
print ("failed to send mail")
It is only function definiton. You have to execut it like
send_email("me#gmai.com", "MyPaSwOrD", "you#gmail.com", "Money for you", "Hi")
But your code will not use this data because it has hardcoded values inside.
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = ['dhe*****#***.com']
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
if __name__ == '__main__':
send_email('abc#gmail.com', 'password', '', 'Hello', 'hello')
I am using this script to send email from abc#gmail.com. (hardcoded the recipient for now)
But I am getting
smtplib.SMTPAuthenticationError: (534, '5.7.14 https://accounts.google.com/signin/ .....
Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 dl17sm699785obc.9 - gsmtp')
Access for Less secure apps is already turned on for that account (abc#gmail.com).
There is also NO 2-factor authentication for that account.
I can't login through a web-browser on that machine because it is an EC2 Instance. How do I proceed here?
I'm using Gmail SMTP to send emails in Python, however sometimes the application may stay idle for an extended period of time.
How do I make sure that the session hasn't expired? Here's my code:
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
Can I always reuse the server session, or can it expire after let's say 5 hours?
So yesterday I had this bit of code written out and it worked perfectly fine, but today it's not sending e-mails anymore. Can someone explain why?
import smtplib
SERVER = 'owa.server.com'
FROM = 'noreply#server.com'
TO = ['person#gmail.com', '1112223344#vtext.com']
name = 'Mr. Man'
SUBJECT = 'Recent Information for: %s' % (name)
TEXT = "Dear " +name+ ",\n\nHello.\n\nSincerely,\nOur Guys Here"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER, 587)
server.ehlo()
server.starttls()
server.ehlo
server.login('noreply#server.com', 'password')
server.sendmail(FROM, TO, message)
server.quit()
This code is a working snippet. I wasn't getting the e-mails in my personal gmail account because gmail was sending it to the spam folder. I checked to see if it works at my office account, and it did just fine.
import smtplib
# Specifying the from and to addresses
fromaddr = 'fromuser#gmail.com'
toaddrs = 'to#gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'username'
password = 'password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Above standard smtp send works with gmail,
thus it must be your server(whatever you're using) configuration that is at fault.
I am trying to send an email in Python using SMTP, with a From address, To address, BCC address, subject, and message. I have the email sending, and it even sends to the BCC as it should, the only issue is that the message of the email says:
To: example#gmail.com
Subject: Subject goes here
this is the email that I’m sending
when I only want the message itself to show where the message belongs, and the subject of the email isn't set, so there's a blank subject. Here is how I have it set up:
def sendEmail(fromAddress, toAddress, bccAddress, appName, message):
subject = "Subject goes here"
BODY = string.join((
"From: %s\r\n" % fromAddress,
"To: %s\r\n" % toAddress,
"Subject: %s\r\n" % subject,
"\r\n",
message
), "\r\n")
#im using arbitrary values here, when I run it I use actual login info
username = 'example#gmail.com'
password = 'password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username,password)
toList = []
bccList = []
toList.append(toAddress)
bccList.append(bccAddress)
server.sendmail(fromAddress, toList + bccList, BODY)
server.quit()
Use the email package (docs).
from email.mime.text import MIMEText
def send_mail(to, from_addr, subject, text):
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to
s = smtplib.SMTP_SSL("smtp.gmail.com")
s.login(smtp_user, smtp_pass)
# for Python 3
s.send_message(msg)
# OR
# for Python 2 (or 3, will still work)
s.sendmail(from_addr, [to], msg.as_string())
s.quit()