Python使用带有列表的YAGMAIL发送邮件

2024-05-23 19:01:37 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我尝试用Python中的yagmail发送邮件,我有一个数组或列表要发送。当我收到邮件时,里面没有任何内容。为什么

import yagmail

keys = []

listToStr = ' '.join([str(elem) for elem in keys]) 


def send(keys):
    print('test')
    yag = yagmail.SMTP('myactualmailishere', 'myactualpassishere')
    yag.send('myactualrecieverishere', 'Test', listToStr)

def on_press(key):
    global keys, count
    count += 1

    if count >= 50:
        count = 0
        send(keys)
        keys = []

Tags: importsend内容列表defcount邮件数组
1条回答
网友
1楼 · 发布于 2024-05-23 19:01:37

因此,在通过^{}发送电子邮件之前,您需要了解以下几点:

  • yagmail^{}之上的包装器库,它是通过python发送电子邮件的标准库
  • 您可以发送Plain Text EmailHTML emails。您的案例看起来更像一个Plain text email

因此,通过yagmail发送邮件在功能上不应与smtplib有所不同

因此,代码应该大致如下所示:

import yagmail

keys = ['a','b','c','d']
listToStr = ' '.join([str(elem) for elem in keys]) 

message = """\
Subject: Hi there. My list is {}.

This message is sent from Python."""

yag = yagmail.SMTP('myactualmailishere', 'myactualpassishere')
yag.send('myactualrecieverishere', 'Test', message.format(listToStr))

这将发送一封普通电子邮件,其中message中的文本和{}替换为 listToStr

尝试上述方法,然后用方法分解代码以实现功能

相关问题 更多 >