python - 使用 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')
解决方案
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')
推荐阅读
- button - 如何在 tinymce-dialog 中设置小写按钮?
- xrdp - Xrdp 0.9.5 是否只支持 IPV6 而不是 IPv4?
- c++ - 我应该如何在向量模板中重载 += 运算符?
- flutter - 如何防止 Android Studio 在我使用之前删除 Flutter 导入?
- r - 如何在 R 中减去 .csv 文件中的 2 列?
- git - 如何查看 vscode 源代码控制的工作变化
- python - 在 Django Admin 中访问内联 get_formset 中的内联字段
- javascript - 如何实现文件上传的恢复暂停功能?我正在使用节点 js、gridfs 和 multer
- javascript - 有没有办法将按钮链接到链接并将其文本居中?
- python - 基于条件的求和列 - 熊猫