Can't save new message to Drafts in gmail - python

I am trying to create and save message to Drafts in gmail, but nothing happen.
import time
import random
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import imaplib
def save_draft(email_to, body, login, password, image):
msg = MIMEMultipart("alternative")
msg.set_charset("utf-8")
msg.attach(MIMEText(body, "plain", "utf-8"))
msg['Subject'] = SUBJECT
msg['From'] = login
msg['To'] = email_to
with open(image, 'rb') as f:
part = MIMEApplication(
f.read(),
Name=image
)
part['Content-Disposition'] = 'attachment; filename={}'.format(IMAGE)
msg.attach(part)
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
imap.login(login, password)
imap.select('[Gmail]/Drafts')
now = imaplib.Time2Internaldate(time.time())
imap.append('[Gmail]/Drafts',
'',
now,
msg.as_bytes())
imap.logout()
When I am changing msg.as_bytes() to msg.as_string(), I got the error below:
TypeError: cannot use a bytes pattern on a string-like object

If you are using Python library here is the code snippet:
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
Once you have created a Message object, you can pass it to the drafts.create method to create a Draft object.
def create_draft(service, user_id, message_body):
"""Create and insert a draft email. Print the returned draft's message and id.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message_body: The body of the email message, including headers.
Returns:
Draft object, including draft id and message meta data.
"""
try:
message = {'message': message_body}
draft = service.users().drafts().create(userId=user_id, body=message).execute()
print 'Draft id: %s\nDraft message: %s' % (draft['id'], draft['message'])
return draft
except errors.HttpError, error:
print 'An error occurred: %s' % error
return None
Also, here is a related SO post that stated: you have decode the bytes object to produce a string.
Hope this helps.

Related

Reply to email using imap email object

I have an email object of an unread email. How do I reply to the email using SMTP lib using the specific email object i.e. (if mail comes from flipkart, the particular mail has an object. I want to reply using that mail object)? I tried using msg['Reply_To'] and I got an error saying the email id is not valid. I did check the smtp documentation and couldn't find anything regarding the same. Here when I comment the [send to] and only add [reply to] the error is shown below. But when I uncomment the same the mail is sent but it is not sent as a reply. This shows that the email id is correct.
CODE:
def SendReply(self):
self.msg["from"] = self.From
self.msg["Cc"] = self.cc
self.msg["To"] = self.To
self.msg["Subject"] = self.Subject
self.Body = str(self.Body)
self.msg["Reply_To"] = self.Reply_To
self.msg.set_content(self.Body)
# print("Print this now", self.msg["Reply_To"])
if self.Attachments is not None:
with open(self.Attachments, "rb") as f:
file_data = f.read()
self.msg.add_attachment(
file_data,
maintype="application",
subtype="xlsx",
filename=self.Attachments,
)
with smtplib.SMTP("smtp.gmail.com", 587) as smtp:
smtp.starttls()
smtp.login(self.From, self.EmailPassword)
self.msg["Reply_To"] = self.Reply_To
smtp.send_message(self.msg)
return ("Mail sent Succesfully", True)
ERROR:
smtplib.SMTPRecipientsRefused:
{'None': (553,
b'5.1.3 The recipient address <None> is not a valid RFC-5321 address.
Learn\n5.1.3 more at\n5.1.3 https://support.google.com/mail/answer/6596 ij25-20020a170902ab5900b0016beceac426sm3191763plb.138 - gsmtp')}

Send email with attachment django

I have the file url in my db. I want to send the file as an attachment in email. I have tried the below code
def mail_business_plan(sender, instance, created, **kwargs):
if created:
ctx = {"ctx":instance}
from_email = 'info#some_email.in'
subject = 'Business Plan by' + instance.company_name
message = get_template('email/business_team.html').render(ctx)
to = ['some_email#gmail.com']
mail = EmailMessage(subject, message, to=to, from_email=from_email)
mail.attach_file(instance.presentation, instance.presentation.read(), instance.presentation.content_type)
return mail.send()
I am getting error as "AttributeError: 'FieldFile' object has no attribute 'content_type'"
What's the best way to send mail with attachment, if the file path is stored in the database.
Assuming you have a model as,
class MyModel(models.Model):
# other fields
presentation = models.FileField(upload_to='some/place/')
and in your signals,
import mimetypes
def mail_business_plan(sender, instance, created, **kwargs):
if created:
ctx = {"ctx": instance}
from_email = 'info#some_email.in'
subject = 'Business Plan by' + instance.company_name
message = get_template('email/business_team.html').render(ctx)
to = ['some_email#gmail.com']
mail = EmailMessage(subject, message, to=to, from_email=from_email)
content_type = mimetypes.guess_type(instance.presentation.name)[0] # change is here <<<
mail.attach_file(instance.presentation, instance.presentation.read(), content_type) # <<< change is here also
return mail.send()
Reference:
mimetypes.guess_type()
Best way to send email in python is using smtp
Here are the steps to configure postfix in ubuntu and send mail.
sudo apt-get install mailutils
just press ok for all the popup (you can change the hostname later)
sudo vim /etc/postfix/main.cf
change the following line from inet_interfaces = all to inet_interfaces = localhost
sudo service postfix restart
Try the following code after you done with all these steps
import os.path
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def send_email(subject, message, from_email, to_email=[], attachment=[]):
"""
:param subject: email subject
:param message: Body content of the email (string), can be HTML/CSS or plain text
:param from_email: Email address from where the email is sent
:param to_email: List of email recipients, example: ["a#a.com", "b#b.com"]
:param attachment: List of attachments, exmaple: ["file1.txt", "file2.txt"]
"""
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ", ".join(to_email)
msg.attach(MIMEText(message, 'html'))
for f in attachment:
with open(f, 'rb') as a_file:
basename = os.path.basename(f)
part = MIMEApplication(a_file.read(), Name=basename)
part[
'Content-Disposition'] = 'attachment; filename="%s"' % basename
msg.attach(part)
email = smtplib.SMTP('localhost')
email.sendmail(from_email, to_email, msg.as_string())
I got the solution by changing
mail.attach_file(instance.presentation, instance.presentation.read(), instance.presentation.content_type)
to
mail.attach_file(instance.presentation.path)

How to change name of Sender in Python Gmail API?

I am working on a script that emails me when I get new grades blah blah. Everything works perfectly except that the sender shows up as my email...
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from email.mime.text import MIMEText
import base64
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string().encode("utf-8")).decode()}
def send_message(message):
global service
user_id = "me"
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message).execute())
return message
except exception as error: # I am aware this line is broken, but it does goes through successfully so this does not execute
print('An error occurred: %s' % error)
# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid or creds == "None":
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
msg = create_message("fname lname", "to#email.com", '', content)
send_message(msg)
That is my code for sending a message, yet my email shows up as the sender when I send a message... How do I fix this?
(I have already tried setting the sender as "John Smith" <from#email.com> and John Smith <from#email.com>)
I just got it working in ruby by setting the mail option:
from: "'some name' <#{variable_of_email_string}>"
Use the following code:
sender = 'John Smith <from#email.com>'
receiver = 'sample#email.com'
subject = 'Subject'
message_text = 'Example of message'
message = create_message(sender, receiver, subject, message_text)

python sending email with text and attachment

I wrote a script for backing up my Neo4J DB.
At the end of the backup process and email is sent to the DB Administrator
The email received without the message_body.
This is the code:
message = MIMEMultipart('alternative')
message['To'] = "Database Admin <%s>" % _receiver
message['From'] = "Pico.buzz Graph Database <%s>" % _sender
if not log.is_error():
message['Subject'] = "Graph DB Backup Finished Successfully"
message_body = 'Successfully backup email. Please see review log file'
else:
message['Subject'] = "ATTENTION: ERROR! Graph DB Backup Failed"
message_body = 'An error occur during backup. please review log'
instance_name = aws.get_instance_name()
instance_details = "Instance Id: %s\nPrivate IP Address: %s" % (aws.get_instance_id(), aws.get_instance_ip())
if instance_name is not None:
instance_details = """Instance Name: %s\n%s""" % (instance_name, instance_details)
message_body = "%s\n\n%s" % (message_body, instance_details)
content = MIMEText(message_body, 'plain')
message.attach(content)
message.attach(_get_log_file())
smtp = smtplib.SMTP('localhost')
smtp.sendmail(_sender, _receiver, message.as_string())
log.info(__name__, "Successfully sent email to: %s" % _receiver)
Any idea why?
MIMEMultipart takes as a parameter to the constructor a multipart subtype.
You were using the subtype 'alternative'. The alternative subtype allows the email to be sent with HTML and text.
You wanted to submit an email with text and an attachment, so you need to construct MIMEMultipart with the subtype 'mixed'.
For more details about these subtypes, you can look at the MIME Wikipedia entry on Multipart messages.

Python smtplib: only the first email out of several arrives to destination

I have put together a function send_email [1] to send emails with support for plain-text and html messages. It works well, but I have an issue I don't quite know how to debug. My system has sendmail as its MTA.
The function is called in a for loop, as follows:
for data in data_set:
subject, message = process(data) # Irrelevant stuff
send_email(subject, message, "fixed#email.address")
In the debug output of smtplib [1] I see that all calls to send_email completed successfully. The weird behavior is:
If the message is "short" (I tested with a single line), all sent messages actually arrive to fixed#email.address
If the message is "not short" (that is, the multiple lines I generate with the real process_data function), only the first email does arrive, while the others don't, even though the debug output of smtplib in [1] reports success for each and all of the emails.
If the message is equally "not short" but the destination address is different for each message, then all messages arrive to their intended destinations.
For the latter case, the for loop would look like:
addresses = ["fixed#email.address", "fixed.2#email.address", ...]
for data, addr in zip(data_set, addresses):
subject, message = process(data) # Irrelevant stuff
send_email(subject, message, addr)
The intended behavior is of course different addresses for different data, but I'm concerned that not understanding why this happens might bite me in an unexpected way later on.
[1] My send mail function:
import smtplib
import socket
import getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email (subject, message, to, reply_to='', cc='', html_message=''):
COMMASPACE = ", "
user, host = get_user_and_host_names()
sender = '%s#%s' % (user, host)
receivers = make_address_list(to)
copies = make_address_list(cc)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = COMMASPACE.join(receivers)
if reply_to:
msg.add_header('Reply-to', reply_to)
if len(copies):
msg.add_header('CC', COMMASPACE.join(copies))
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
if message:
msg.attach( MIMEText(message, 'plain'))
if html_message:
msg.attach( MIMEText(html_message, 'html'))
smtpObj = smtplib.SMTP('localhost')
smtpObj.set_debuglevel(1)
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
print "\nSuccessfully sent email to:", COMMASPACE.join(receivers)
def get_user_and_host_names():
user = getpass.getuser()
host = socket.gethostname()
return user, host
def make_address_list (addresses):
if isinstance(addresses, str):
receivers = addresses.replace(' ','').split(',')
elif isinstance(addresses, list):
receivers = addresses
return receivers
A working solution I had for sending email:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import smtplib
class EMail(object):
""" Class defines method to send email
"""
def __init__(self, mailFrom, server, usrname, password, files, debug=False):
self.debug = debug
self.mailFrom = mailFrom
self.smtpserver = server
self.EMAIL_PORT = 587
self.usrname = usrname
self.password = password
def sendMessage(self, subject, msgContent, files, mailto):
""" Send the email message
Args:
subject(string): subject for the email
msgContent(string): email message Content
files(List): list of files to be attached
mailto(string): email address to be sent to
"""
msg = self.prepareMail(subject, msgContent, files, mailto)
# connect to server and send email
server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
server.ehlo()
# use encrypted SSL mode
server.starttls()
# to make starttls work
server.ehlo()
server.login(self.usrname, self.password)
server.set_debuglevel(self.debug)
try:
failed = server.sendmail(self.mailFrom, mailto, msg.as_string())
except Exception as er:
print er
finally:
server.quit()
def prepareMail(self, subject, msgHTML, attachments, mailto):
""" Prepare the email to send
Args:
subject(string): subject of the email.
msgHTML(string): HTML formatted email message Content.
attachments(List): list of file paths to be attached with email.
"""
msg = MIMEMultipart()
msg['From'] = self.mailFrom
msg['To'] = mailto
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
#the Body message
msg.attach(MIMEText(msgHTML, 'html'))
msg.attach(MIMEText("Add signature here"))
if attachments:
for phile in attachments:
# we could check for MIMETypes here
part = MIMEBase('application',"octet-stream")
part.set_payload(open(phile, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(phile))
msg.attach(part)
return msg
I hope this helps.
My Working solution to format your list in this manner (after many hours of experiment):
["firstemail#mail.com", "secondmail#mail.com", "thirdmail#email.com"]
I used:
to_address = list(str(self.to_address_list).split(","))
to convert my QString into string then into list with splitting with a ","
In my case COMMASPACE was not working, because on splitting a space was already added by default.

Categories