1

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", "[email protected]", '', 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" <[email protected]> and John Smith <[email protected]>)

3
  • Have you checked this SO post? Commented May 24, 2018 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 Commented May 24, 2018 at 14:57
  • Off-Topic: You can fix line 51 by writing "Exception" with a capital letter: except Exception as error: Commented Aug 25, 2021 at 7:39

2 Answers 2

3

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

from: "'some name' <#{variable_of_email_string}>"
Sign up to request clarification or add additional context in comments.

Comments

2

Use the following code:

sender = 'John Smith <[email protected]>'
receiver = '[email protected]'
subject = 'Subject'
message_text = 'Example of message'

message = create_message(sender, receiver, subject, message_text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.