1

I'm trying to send an email via the Gmail API using Python, I want to iclude the signature. I'm able to successfuly get the signature using the following funciotn:

def addSignature(service):
    sigtanture = service.users().settings().sendAs().get(userId=USER, sendAsEmail='example@example.com').execute()
    return sigtanture['signature']

addSignature(SERVICE)

However, at the time of creating the message that I'll send, how do I append the Signature?

def CreateMessage(sender, to, subject, message_text, signature):
    message = MIMEMultipart('alternative')
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    message[???] = signature

    raw = base64.urlsafe_b64encode(message.as_bytes())
    raw = raw.decode()
    body = {'raw': raw}
    return body

I'm trying to send the message:

thebody = CreateMessage(USER, 'to@example.com', 'TestSignatureAPI','testing', addSignature(SERVICE))
SendMessage(SERVICE, USER, thebody)

Thank you!

Axel
  • 98
  • 1
  • 9

1 Answers1

1

Unfortunately there is no built-in feature to append the Gmail signature to your email

Instead, once you retrieve your signature as a text, you can simply append it manually to the end of your message_text before encoding.

See also here.

Assuming you retrieved the signature correctly and based on your code this means:

  def CreateMessage(sender, to, subject, message_text, signature):
    message_text = message_text + "\n" + signature
    message = MIMEMultipart('alternative')
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    
    message.attach(MIMEText(message_text_plain, 'plain'))
    
    raw = base64.urlsafe_b64encode(message.as_bytes())
    raw = raw.decode()
    body = {'raw': raw}
    return body
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • 1
    Thank you for clarifying, would you mind to place an example of how can it be append manually? Thank you – Axel Jun 24 '20 at 14:13