sanjihan
sanjihan

Reputation: 6014

set sender name and last name when sending mail with Gmail API

How do you set sender Name and Last name when sending mail via gmail API?

I use this code: https://github.com/chris-brown-nz/python-gmail-api

The code is great and I successfully send mail. However, the name of sender is just the text left from @ sign. example: if John Travis send email from [email protected] the name that appears on the receiver side would be johnhomeboy, instead of John Travis. What property do I need to set in order to send emails with my name and last name?

Upvotes: 10

Views: 4403

Answers (1)

Alessandro Solbiati
Alessandro Solbiati

Reputation: 979

As mentioned in the comments by AChampion, gmail API accepts standard RFC822 formats so, using Gmail Python APIs sending from a email [email protected]

create_message('[email protected]', 'DESTINATION_EMAIL', 'TITLE', 'BODY')

Sender name can be customised with


create_message("CUSTOMTISED_FROM_TITLE '<[email protected]>'", 'DESTINATION_EMAIL', 'TITLE', 'BODY')

Where create_message is defined as

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

Following the example at https://developers.google.com/gmail/api/guides/sending?authuser=2

Upvotes: 4

Related Questions