def main(): """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. file: The path to the file to be attached. """ gmail.create_message_with_attachment('me', '*****@*****.**', 'Test email with attachment', "Contents of the body", 'test-attachment.xls')
def job(): print("Exporting New Relic dashboard...") dashboard = exporter( guid=config["dashboard"]["guid"], file_type=config["dashboard"]["file_type"], width=int(config["dashboard"]["width"]), height=int(config["dashboard"]["height"]), ) print("Authenticating with Google...") service = get_service() for email in config["email"]["to"]: print("Creating message...") message = create_message_with_attachment( sender=config["email"]["sender"], to=email, subject=config["email"]["subject"], message_text=config["email"]["text"], file=dashboard, ) print("Sending email...") send_email(message=message, user_id="me", session=service) print("Done!")
def main(email_address, email_friendly_name, email_subject): """Writes the CSV data to the csv file, then opens a connection to gmail and sends the email with that attachment""" with open(FILENAME, 'w', newline='', encoding='utf-8-sig') as csvfile: writer = csv.writer(csvfile) writer.writerows(CSV_DATA) service = gmail.setup() sender_email = email_address email_from = email_friendly_name + ' <' + sender_email +'>' for email in EMAILS: email_to = email message = gmail.create_message_with_attachment(email_from, email_to, email_subject, EMAIL_BODY, FILENAME) gmail.send_message(service, sender_email, message) time.sleep(0.5)
# Initial run On the first run of send or load you'll be directed in browser to allow access for you gmail account. This creates a token in **token.pickle** file which is used to all sub-sequent calls. Refresh token have to be re-created on password change. # Send example # python send.py or import gmail gmail.create_message_with_attachment('me', '*****@*****.**', 'Test email', "Contents of the body", 'test-attachment.xls') Will send message from your account to **[email protected]** with subject **"Test email"**, body **"Contents of the body"**, and will attach local file **"test-attachment.xls"** # Load example # python load.py or import gmail gmail.load_unread('files', ['*****@*****.**'], "attachments_loaded") Will download attachments from unread emails with "*****@*****.**" sender, save them in **"files"** folder and apply label **"attachments_loaded"** to them.
def test_create_message_with_attachment(self): """Check to see that the length of the base64 raw message is 448, as it should be with these input values""" generated_message = gmail.create_message_with_attachment( '*****@*****.**', '*****@*****.**', 'Test Subject', 'Message Text') self.assertEqual(len(generated_message['raw']), 448)