J. Doe
J. Doe

Reputation: 41

Send an email as a google group from python

I want to send email in python and the following code works. However I want to send the email as a google group. Since a google group has no password, I am not able to login to the server. Is there anyway I can go about this?

def sendEmail(self, toEmail, subject, message ):
    msg = MIMEMultipart()
    password = "*****"
    msg['From'] = "[email protected]"
    msg['To'] = toEmail
    msg['Subject'] = subject
    msg.attach(MIMEText(message, 'plain'))
    server = smtplib.SMTP('smtp.gmail.com: 587')
    server.starttls()
    server.login(msg['From'],password)

    server.sendmail(msg['From'], msg['To'].split(","), msg.as_string())
    server.quit()
    logging.debug('sent email to %s', (msg['To']))

Upvotes: 4

Views: 2695

Answers (2)

Ki Rill
Ki Rill

Reputation: 46

You need to add group address as allowed send-as address in Gmail settings. Then you'll be able to use group address as "From" address. Authentication should still be done with your (non-group) address, of course. Screenshot

Upvotes: 0

Sami Start
Sami Start

Reputation: 2025

It sends like you should use a Service Account to send the email on your behalf.

Here is a pretty good guide (not written by me): https://medium.com/lyfepedia/sending-emails-with-gmail-api-and-python-49474e32c81f

Upvotes: 1

Related Questions