首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Smtplib,如何设置主题

Python Smtplib,如何设置主题
EN

Stack Overflow用户
提问于 2021-09-01 07:24:42
回答 1查看 490关注 0票数 0

好的,我试着用smtplib发送一封电子邮件,里面的代码是有效的!但是我不知道怎么把一个主题放进去,我见过其他人在这里谈论它,但是他们用的方法使得你不能在里面放任何文字,所以我在这里问。

此外,发件人是一个gmail帐户,与接收者相同。

代码语言:javascript
复制
import smtplib
import socket

email='xxxxxxxxxx'
password='xxxxxxxxxx'
    
    
def sendMail(to, subject, message):
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        print("Connection Status: Connected")
    except socket.gaierror:
        print("Connection Status: ERROR: Not connected to the internet")
    server.ehlo()
    server.starttls()
    server.login(email, password)
    # subject here or something
    server.sendmail('text', to, message)
    
try:
    sendMail('xxxxxxxx', 'this is the text')
except smtplib.SMTPAuthenticationError:
    print("ERROR: Username or Password are not accepted")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-01 07:37:39

代码语言:javascript
复制
import smtplib
import socket


message = f'Subject: {SUBJECT}\n\n{TEXT}'
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69009418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档