0

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>)

  • Have you checked this [SO post](https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python/27515833#27515833)? – MαπμQμαπkγVπ.0 May 24 '18 at 10:45
  • My code works without any errors whatsoever. Emails send without a hitch, the only problem is that the sender shows up as my email instead of my name... @MαπμQμαπkγVπ.0 – Kyle Donoghue May 24 '18 at 14:57
  • Off-Topic: You can fix line 51 by writing "Exception" with a capital letter: except Exception as error: – Mare Seestern Aug 25 '21 at 07:39

2 Answers2

3

I just got it working in ruby by setting the mail option:

from: "'some name' <#{variable_of_email_string}>"
Ekkstein
  • 771
  • 11
  • 20
2

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)