Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 334
sanjihan Avatar asked Dec 30 '16 15:12

sanjihan


People also ask

How do I send HTML formatted emails through the Gmail API?

message = MIMEText(message_text, 'html') <-- add the 'html' as the second parameter of the MIMEText object constructor.


1 Answers

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

like image 91
Alessandro Solbiati Avatar answered Nov 14 '22 23:11

Alessandro Solbiati