Sending emails with Gmail OAuth2 and SMTP (Python) - python

I am developing an application that allows a user to authenticate with Gmail and send an email.
Here is what needs to happen:
user authenticates with OAuth2
user's email address (or any other persistent unique identifier) is obtained and used to log the user into the app
user can compose and send an email using SMTP
However, I'm running into a problem with authentication. The function I am using is OAuth2WebServerFlow():
flow = OAuth2WebServerFlow(client_id=GOOGLE_CLIENT_ID,
client_secret=GOOGLE_CLIENT_SECRET,
scope=scope,
redirect_uri=base_app_url + '/oauth2callback')
This works well when scope='https://mail.google.com/', but the problem is I can't obtain the user's email address because the user hasn't given that permission (as indicated by https://developers.google.com/oauthplayground/). As a result, I've had to hardcode my own email address to get this to work.
On the other hand, when scope='https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email', I can get the user's email address with the following:
r = requests.get('https://www.googleapis.com/oauth2/v2/userinfo',
headers={'Authorization': 'OAuth ' + access_token})
The problem in this case, however, is that when I go to send an email, the access_token that is used to create the auth string is not accepted, giving me the following error (which I believe is an inappropriate one):
SMTPSenderRefused: (535,
'5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 cv19sm54718503vdb.5',
u'<MY_NAME#MY_GOOGLE_APP_DOMAIN.COM>')
Here is my code for sending the email:
def send_email(user_address, access_token, recipient_address, subject, body):
xoauth2_string = 'user=%s\1auth=Bearer %s\1\1' % (user_address, access_token)
url = "https://mail.google.com/mail/b/" + user_address + "/smtp/"
conn = smtplib.SMTP('smtp.gmail.com', 587)
conn.set_debuglevel(True)
conn.ehlo()
conn.starttls()
conn.ehlo()
conn.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(xoauth2_string))
header = 'To:' + recipient_address + '\n'
header += 'From:' + user_address + '\n'
header += 'Subject:' + subject + ' \n'
header += 'Content-Type: text/html; charset=UTF-8\n'
msg = header + '\n ' + body + ' \n\n'
conn.sendmail(user_address, recipient_address, msg)
Basically I want to find out if:
I can get a user's email or some other persistent unique identifier with scope='https://mail.google.com/'.
There is a way to make SMTP work with scope='https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email'.
Why are my auth strings getting rejected? Is there something I'm missing? Is there something wrong with the google api when requesting multiple permissions?

Related

Error "email has no attribute encode" sending email in python

I am trying to send an email using MIME in python. Below is the code I am using :
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
try:
raw_data = request.get_json()
pwd_email_id = raw_data['email']
log.error("Sending email to {}".format(pwd_email_id))
recipients = pwd_email_id
msg = MIMEMultipart()
msg['Subject'] = 'App Registered'
msg['From'] = 'xyz#gmail.com'
msg['To'] = email
message_text = "Dear " + pwd_email_id + "\r\n\r\nYour app has been registered successfully.\r\nBelow are the " \
"details:\r\n\r\n\r\n1.App Name: " + "app_name" + "\r\n2.App Key: " + "app_key" + "\r\n3.Registered Date: " + "registered_date" + "\r\n4.Expiry Date: " + "expiry_date" + "\r\n\r\n\r\nUse app name and app key as headers to make calls to services. " \
"Do not share your app key with anyone.\r\nLet us know if you face any issues.\r\n\r\nThanks "
text = MIMEText(message_text)
msg.attach(text)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login('xyz#gmail.com', '<password>')
s.sendmail("xyz#gmail.com", recipients, msg.as_string())
s.quit()
except Exception as e:
log.error("Exception in sending email {}".format(e))
but its giving me the below error:
module email has no attribute encode
at line:
s.sendmail("xyz#gmail.com", recipients, msg.as_bytes())
I am not able to understand why its giving this error. I have tried only using msg instead of msg.as_bytes() but its still the same. Can anyone please point out the issue in the code. Thanks
Looks like a typo to me.
You have assigned the module email when calling msg['To'] = email. That module must have been imported outside of the shared code (check your imports, it's probably there!). msg.as_string() is simply having trouble parsing the module object (since modules don't have the encode attribute).
I just had the same problem. This error is because input in msg['To'] is the variable email, but you didn't create this variable.
OBS: var email needs to have the email of the person you want to send the msg to. This way, it will be possible to send the message from yours to someone's email.

SMTP works in Linux but not on Mac [duplicate]

I am trying to send email (Gmail) using python, but I am getting following error.
Traceback (most recent call last):
File "emailSend.py", line 14, in <module>
server.login(username,password)
File "/usr/lib/python2.5/smtplib.py", line 554, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
The Python script is the following.
import smtplib
fromaddr = 'user_me#gmail.com'
toaddrs = 'user_you#gmail.com'
msg = 'Why,Oh why!'
username = 'user_me#gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
def send_email(user, pwd, recipient, subject, body):
import smtplib
FROM = user
TO = recipient if isinstance(recipient, 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(user, pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
if you want to use Port 465 you have to create an SMTP_SSL object:
# SMTP_SSL Example
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(gmail_user, gmail_pwd)
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
server_ssl.sendmail(FROM, TO, message)
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'
You need to say EHLO before just running straight into STARTTLS:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
Also you should really create From:, To: and Subject: message headers, separated from the message body by a blank line and use CRLF as EOL markers.
E.g.
msg = "\r\n".join([
"From: user_me#gmail.com",
"To: user_you#gmail.com",
"Subject: Just a message",
"",
"Why, oh why"
])
Note:
In order for this to work you need to enable "Allow less secure apps" option in your gmail account configuration. Otherwise you will get a "critical security alert" when gmail detects that a non-Google apps is trying to login your account.
I ran into a similar problem and stumbled on this question. I got an SMTP Authentication Error but my user name / pass was correct. Here is what fixed it. I read this:
https://support.google.com/accounts/answer/6010255
In a nutshell, google is not allowing you to log in via smtplib because it has flagged this sort of login as "less secure", so what you have to do is go to this link while you're logged in to your google account, and allow the access:
https://www.google.com/settings/security/lesssecureapps
Once that is set (see my screenshot below), it should work.
Login now works:
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('me#gmail.com', 'me_pass')
Response after change:
(235, '2.7.0 Accepted')
Response prior:
smtplib.SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 g66sm2224117qgf.37 - gsmtp')
Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:
https://accounts.google.com/DisplayUnlockCaptcha
Click continue and this should give you 10 minutes for registering your new app. So proceed to doing another login attempt now and it should work.
UPDATE: This doesn't seem to work right away you may be stuck for a while getting this error in smptlib:
235 == 'Authentication successful'
503 == 'Error: already authenticated'
The message says to use the browser to sign in:
SMTPAuthenticationError: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at\n5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qo11sm4014232igb.17 - gsmtp')
After enabling 'lesssecureapps', go for a coffee, come back, and try the 'DisplayUnlockCaptcha' link again. From user experience, it may take up to an hour for the change to kick in. Then try the sign-in process again.
This Works
Create Gmail APP Password!
After you create that then create a file called sendgmail.py
Then add this code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : Jeromie Kirchoff
# Created Date: Mon Aug 02 17:46:00 PDT 2018
# =============================================================================
# Imports
# =============================================================================
import smtplib
# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
gmail_user = 'THEFROM#gmail.com'
gmail_app_password = 'YOUR-GOOGLE-APPLICATION-PASSWORD!!!!'
# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = ['THE-TO#gmail.com', 'THE-TO#gmail.com']
sent_subject = "Hey Friends!"
sent_body = ("Hey, what's up? friend!\n\n"
"I hope you have been well!\n"
"\n"
"Cheers,\n"
"Jay\n")
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)
# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_app_password)
server.sendmail(sent_from, sent_to, email_text)
server.close()
print('Email sent!')
except Exception as exception:
print("Error: %s!\n\n" % exception)
So, if you are successful, will see an image like this:
I tested by sending an email from and to myself.
Note: I have 2-Step Verification enabled on my account. App Password works with this! (for gmail smtp setup, you must go to https://support.google.com/accounts/answer/185833?hl=en and follow the below steps)
This setting is not available for accounts with 2-Step Verification enabled. Such accounts require an application-specific password for less secure apps access.
Clarification
Navigate to https://myaccount.google.com/apppasswords and create an APP Password as stated above.
You down with OOP?
#!/usr/bin/env python
import smtplib
class Gmail(object):
def __init__(self, email, password):
self.email = email
self.password = password
self.server = 'smtp.gmail.com'
self.port = 587
session = smtplib.SMTP(self.server, self.port)
session.ehlo()
session.starttls()
session.ehlo
session.login(self.email, self.password)
self.session = session
def send_message(self, subject, body):
''' This must be removed '''
headers = [
"From: " + self.email,
"Subject: " + subject,
"To: " + self.email,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
self.session.sendmail(
self.email,
self.email,
headers + "\r\n\r\n" + body)
gm = Gmail('Your Email', 'Password')
gm.send_message('Subject', 'Message')
Here is a Gmail API example. Although more complicated, this is the only method I found that works in 2019. This example was taken and modified from:
https://developers.google.com/gmail/api/guides/sending
You'll need create a project with Google's API interfaces through their website. Next you'll need to enable the GMAIL API for your app. Create credentials and then download those creds, save it as credentials.json.
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from email.mime.text import MIMEText
import base64
#pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
def create_message(sender, to, subject, msg):
message = MIMEText(msg)
message['to'] = to
message['from'] = sender
message['subject'] = subject
# Base 64 encode
b64_bytes = base64.urlsafe_b64encode(message.as_bytes())
b64_string = b64_bytes.decode()
return {'raw': b64_string}
#return {'raw': base64.urlsafe_b64encode(message.as_string())}
def send_message(service, user_id, message):
#try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
print( 'Message Id: %s' % message['id'] )
return message
#except errors.HttpError, error:print( 'An error occurred: %s' % error )
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Example read operation
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
# Example write
msg = create_message("from#gmail.com", "to#gmail.com", "Subject", "Msg")
send_message( service, 'me', msg)
if __name__ == '__main__':
main()
Not directly related but still worth pointing out is that my package tries to make sending gmail messages really quick and painless. It also tries to maintain a list of errors and tries to point to the solution immediately.
It would literally only need this code to do exactly what you wrote:
import yagmail
yag = yagmail.SMTP('user_me#gmail.com')
yag.send('user_you#gmail.com', 'Why,Oh why!')
Or a one liner:
yagmail.SMTP('user_me#gmail.com').send('user_you#gmail.com', 'Why,Oh why!')
For the package/installation please look at git or pip, available for both Python 2 and 3.
You can find it here: http://jayrambhia.com/blog/send-emails-using-python
smtp_host = 'smtp.gmail.com'
smtp_port = 587
server = smtplib.SMTP()
server.connect(smtp_host,smtp_port)
server.ehlo()
server.starttls()
server.login(user,passw)
fromaddr = raw_input('Send mail by the name of: ')
tolist = raw_input('To: ').split()
sub = raw_input('Subject: ')
msg = email.MIMEMultipart.MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = email.Utils.COMMASPACE.join(tolist)
msg['Subject'] = sub
msg.attach(MIMEText(raw_input('Body: ')))
msg.attach(MIMEText('\nsent via python', 'plain'))
server.sendmail(user,tolist,msg.as_string())
Dec, 2022 Update:
You need to use an app password to allow your app to access your google account.
Sign in with App Passwords:
An App Password is a 16-digit passcode that gives a less secure app or
device permission to access your Google Account. App Passwords can
only be used with accounts that have 2-Step Verification turned on.
In addition, google hasn't allowed your app to access your google account with username(email address) and password since May 30, 2022. So now, you need username(email address) and an app password to access your google account.
Less secure apps & your Google Account:
To help keep your account secure, from May 30, 2022, ​​Google no
longer supports the use of third-party apps or devices which ask you
to sign in to your Google Account using only your username and
password.
How to generate an app password:
First, click on Account from 9 dots:
Then, click on App passwords from Security. *Don't forget to turn on 2-Step Verification before generating an app password otherwise you cannot generate an app password:
Then, click on Other (Custom name):
Then, put your app name, then click on GENERATE:
Finally, you could generate the app password xylnudjdiwpojwzm:
So, your code with the app password above is as shown below:
import smtplib
fromaddr = 'user_me#gmail.com'
toaddrs = 'user_you#gmail.com'
msg = 'Why,Oh why!'
username = 'user_me#gmail.com'
password = 'xylnudjdiwpojwzm' # Here
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
In addition, settings.py with the app password above in Django is as shown below:
# "settings.py"
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'myaccount#gmail.com'
EMAIL_HOST_PASSWORD = 'xylnudjdiwpojwzm' # Here
Realized how painful many of the things are with sending emails via Python thus I made an extensive library for it. It also has Gmail pre-configured (so you don't have to remember Gmail's host and port):
from redmail import gmail
gmail.user_name = "you#gmail.com"
gmail.password = "<YOUR APPLICATION PASSWORD>"
# Send an email
gmail.send(
subject="An example email",
receivers=["recipient#example.com"],
text="Hi, this is text body.",
html="<h1>Hi, this is HTML body.</h1>"
)
Of course you need to configure your Gmail account (don't worry, it's simple):
Set up 2-step-verification (if not yet set up)
Create an Application password
Put the Application password to the gmail object and done!
Red Mail is actually pretty extensive (include attachments, embed images, send with cc and bcc, template with Jinja etc.) and should hopefully be all you need from an email sender. It is also well tested and documented. I hope you find it useful.
To install:
pip install redmail
Documentation: https://red-mail.readthedocs.io/en/latest/
Source code: https://github.com/Miksus/red-mail
Note that Gmail don't allow changing the sender. The sender address is always you.
Enable less secure apps on your gmail account and use (Python>=3.6):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmailUser = 'XXXXX#gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX#gmail.com'
message = f"""
Type your message here...
"""
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Subject here..."
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')
There is a gmail API now, which lets you send email, read email and create drafts via REST.
Unlike the SMTP calls, it is non-blocking which can be a good thing for thread-based webservers sending email in the request thread (like python webservers). The API is also quite powerful.
Of course, email should be handed off to a non-webserver queue, but it's nice to have options.
It's easiest to setup if you have Google Apps administrator rights on the domain, because then you can give blanket permission to your client. Otherwise you have to fiddle with OAuth authentication and permission.
Here is a gist demonstrating it:
https://gist.github.com/timrichardson/1154e29174926e462b7a
great answer from #David, here is for Python 3 without the generic try-except:
def send_email(user, password, recipient, subject, body):
gmail_user = user
gmail_pwd = password
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)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
Seems like problem of the old smtplib. In python2.7 everything works fine.
Update: Yep, server.ehlo() also could help.
import smtplib
fromadd='from#gmail.com'
toadd='send#gmail.com'
msg='''hi,how r u'''
username='abc#gmail.com'
passwd='password'
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,passwd)
server.sendmail(fromadd,toadd,msg)
print("Mail Send Successfully")
server.quit()
except:
print("Error:unable to send mail")
NOTE:https://www.google.com/settings/security/lesssecureapps that should be enabled
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("fromaddress", "password")
msg = "HI!"
server.sendmail("fromaddress", "receiveraddress", msg)
server.quit()

Correct format for IMAP append command in Python? (Yahoo mail)

The following Python function works for outlook, gmail and my shared hosting exim server but when sending mail through yahoo.com it returns this error:
APPEND command error: BAD ['[CLIENTBUG] Additional arguments found after last expected argument']. Data: FHDJ4 APPEND inbox.sent "31-Aug-2016 12:30:45 +0100" {155}
For comparison, outlook returns:
('OK', ['[APPENDUID 105 2] APPEND completed.'])
Gmail returns:
('OK', ['[APPENDUID 14 2] (Success)'])
and Exim returns:
('OK', ['[APPENDUID 1472211409 44] Append completed (0.788 + 0.076 secs).'])
My function uses imaplib2, the arguments passed to it are all strings, and self.username is the sending email address as address#domain.com
My function is:
def send_mail(self, to_addrs, subject, msgtext, verbose=False):
# build message to send
msg = email.message.Message()
msg.set_unixfrom('pymotw')
msg['From'] = self.username
msg['To'] = to_addrs
msg['Subject'] = subject
msg.set_payload(msgtext)
if verbose: print("Sending Mail:\n ", msg)
# connect and send message
server = self.connect_smtp()
server.ehlo()
server.login(self.username, self.password)
server.sendmail(self.username, to_addrs, str(msg))
server.quit()
print("Saving mail to sent")
sentbox_connection = self.imap_connection
print(sentbox_connection.select('inbox.sent'))
print(sentbox_connection.append('inbox.sent', None, imaplib2.Time2Internaldate(time.time()) , str(msg)))
I've tried generating the msg variable with this line instead:
msg = "From: %s\r\n" % self.username + "To: %s\r\n" % to_addrs + "Subject: %s\r\n" % subject + "\r\n" + msgtext
and appending the message using "" instead of None like so:
print(sentbox_connection.append('inbox.sent', None, imaplib2.Time2Internaldate(time.time()) , str(msg)))
Can you tell me what I'm doing wrong? Or if Yahoo has a specific way of handling append commands that I need to account for?
Edit: To clarify, sending the mail works OK for all smtp servers, but appending the sent mail to inbox.sent fails for yahoo
I've resolved this. I noticed the message text did not end with CRLF. Other mail servers were appending this serverside to accept the command, Yahoo does not. The below now works.
I've amended the message payload line to:
msg.set_payload("%s \r\n" % msgtext) # Yahoo is strict with CRLF at end of IMAP command

SMTP with Python, can't send emails to domains other than gmail

I'm trying to send an email using a Python script and smtp (I made an account on sendgrid.com), and I found this code on http://www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/ and I can make it work just fine for gmail, but no other domain seems to receive their test mail. When I check my email activity on sendgrid.com, it tells me the emails have been dropped or bounced because they aren't RFC 5322 compliant. I tried to google this error, but I just can't seem to find a solution.
This is what I have so far:
import smtplib
to = 'example#hotmail.com'
user = 'username'
pwd = 'password'
smtpserver = smtplib.SMTP("smtp.sendgrid.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(user, pwd)
header = 'To:' + to + '\n' + 'From: ' + user + '\n' + 'Subject:Test! \n'
print header
msg = header + '\n Test message \n\n'
smtpserver.sendmail(user, to, msg)
print 'Done!'
smtpserver.close()
Feel free to help me out!

Sending mails to multiple users using python

I recently found a code to send emails using python. It was only for a single user so I modified it to take emails from a txt file which stores the email on every line and then send them mails. However what I found is that the mails end up in the spam folder(in case of Gmail) or the Junk folder (in case of hotmail or live). Is it possible to change the code so that the message lands in the inbox instead of being filtered as spam? Did I get something wrong?
import smtplib,sys
server = 'smtp.gmail.com'
port = 587
sender = 'my-username#gmail.com'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
"Sends an e-mail to the specified recipient."
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, 'my-password!')
f = open('emails.txt')
for line in f:
recipient = line
print recipient
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient]
headers = "\r\n".join(headers)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
f.close()
session.quit()
That's a very difficult question, because the spam classification is not done by you. (Obviously! If anyone could make their messages "not spam" then of course the spammers would do that too.)
There are various things you should do if you are seriously thinking about sending large-scale email, involving authenticating servers etc. Unless you are an expert, you should engage the services of a mailing company to do them.
I had a similar problem using PHP to send emails and I was able to get my emails out of the spam folder just by changing the subject and body - making them slightly more meaningful and less test-like.
Try out different things - a subject like "Invoice from Jack's Store" or "Introducing you to Twitter." Or just take the subject and the body from an actual email and put it in your test.
Yes One Way is there but there is time wasting method.....
This is your Code:
put the sleep method and replace with this code
import smtplib,sys
import time
server = 'smtp.gmail.com'
port = 587
sender = 'my-username#gmail.com'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
"Sends an e-mail to the specified recipient."
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, 'my-password!')
f = open('emails.txt')
for line in f:
recipient = line
print recipient
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient]
headers = "\r\n".join(headers)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
time.sleep(3)
f.close()
session.quit()
IF any other problem comment....:)

Categories

Resources