首页 > 解决方案 > 使用 Google Colab 发送邮件

问题描述

我正在尝试使用 google colab 和 google sheet 发送多封电子邮件,其中电子邮件正文包含由 google sheet 读取的值。所以我想在我的邮件中打印 B_NAME(gsheet 中的列)的值,但是在我收到的邮件中 B_NAME 列的值没有打印出来。那么我怎样才能阅读那个特定的列并在我的脚本的 html 中使用它呢?

from gspread_dataframe import get_as_dataframe, set_with_dataframe
    import smtplib
    import pandas as pd
    from smtplib import SMTPException
    from email.message import EmailMessage
    from datetime import date
    from datetime import datetime, timedelta
    from google.colab import auth
    auth.authenticate_user()
    import gspread
    from oauth2client.client import GoogleCredentials
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    gc = gspread.authorize(GoogleCredentials.get_application_default())
    today = date.today()- timedelta(days=3)
    # dd/mm/YY
    current_date = today.strftime("%B %d, %Y")
    excelfile = gc.open('testdata1').sheet1
    row_values_list = excelfile.get_all_records()
    for row_value in row_values_list:
      receiver_address = row_value.get('EMAIL_ADDRESS')
      B_NAME = row_value.get('BUSINESS_NAME')
      BUNIT_NUMBER = row_value.get('BUNIT_NUMBER')
     # print(B_NAME)
      #print(receiver_address)
      #print(BUNIT_NUMBER)
    # Create message container - the correct MIME type is multipart/alternative.
    sender_address = "sendsautomail@gmail.com"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Test Mail"
    msg['From'] = sender_address
    msg['To'] = receiver_address
    # Create the body of the message (a plain-text and an HTML version).
    text = "Hi!"
    html = """\
    <html>
    <body>
    Hi """ + str(B_NAME) + """ , this is a test mail using Google Colab.<br>
    Regards<br>
    GWorks Team
    </body>
    </html>
    """
    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)
    # Send the message via local SMTP server.
    s = smtplib.SMTP_SSL('smtp.gmail.com', port = 465)
    #s.connect("smtp.gmail.com",465)
    # sendmail function takes 3 arguments: sender's address, recipient's address
    # and message to send - here it is sent as one string.
    s.login('sendsautomail@gmail.com', 'sepvcnweortewpvdx')
    s.sendmail(sender_address, receiver_address, msg.as_string())
    s.quit()
    print('Mail Sent')
   

标签: pythongoogle-sheetsgoogle-colaboratory

解决方案


from gspread_dataframe import get_as_dataframe, set_with_dataframe
    import smtplib
    import pandas as pd
    from smtplib import SMTPException
    from email.message import EmailMessage
    from datetime import date
    from datetime import datetime, timedelta
    from google.colab import auth
    auth.authenticate_user()
    import gspread
    from oauth2client.client import GoogleCredentials
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    gc = gspread.authorize(GoogleCredentials.get_application_default())
    today = date.today()- timedelta(days=3)
    # dd/mm/YY
    current_date = today.strftime("%B %d, %Y")
    excelfile = gc.open('testdata1').sheet1
    row_values_list = excelfile.get_all_records()
    for row_value in row_values_list:
      receiver_address = row_value.get('EMAIL_ADDRESS')
      B_NAME = row_value.get('BUSINESS_NAME')
      BUNIT_NUMBER = row_value.get('BUNIT_NUMBER')
     # print(B_NAME)
      #print(receiver_address)
      #print(BUNIT_NUMBER)
    # Create message container - the correct MIME type is multipart/alternative.
    sender_address = "sendsautomail@gmail.com"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Test Mail"
    msg['From'] = sender_address
    msg['To'] = receiver_address
    # Create the body of the message (a plain-text and an HTML version).
    text = "Hi!"
    html = f"""\
    <html>
    <body>
    Hi """ + {str(B_NAME)} + """ , this is a test mail using Google Colab.<br>
    Regards<br>
    GWorks Team
    </body>
    </html>
    """
    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)
    # Send the message via local SMTP server.
    s = smtplib.SMTP_SSL('smtp.gmail.com', port = 465)
    #s.connect("smtp.gmail.com",465)
    # sendmail function takes 3 arguments: sender's address, recipient's address
    # and message to send - here it is sent as one string.
    s.login('sendsautomail@gmail.com', 'sepvcnweortewpvdx')
    s.sendmail(sender_address, receiver_address, msg.as_string())
    s.quit()
    print('Mail Sent')


推荐阅读