为了账号安全,请及时绑定邮箱和手机立即绑定

启动桌面邮件程序并使用 python 启动一个空白电子邮件

启动桌面邮件程序并使用 python 启动一个空白电子邮件

POPMUISE 2022-06-02 11:30:47
我正在尝试启动本地邮件应用程序、Windows 上的 Outlook 365 和 MacOS 上的 Mail,并启动一封空白电子邮件,其中 (To: ) 列从数据库查询中填写。理想情况下,它看起来类似于:def email_create():     if MacOS:        open Mail.App        To: = [sql statement]     if Windows        open Outlook.application        To: = [sql statement]编辑:在下面@spYder 的帮助下,我只想发布我的最终产品,看起来效果很好。再次感谢@spYder。import webbrowser    def send_email_command():        sql = "SELECT EMAIL_PRIMARY FROM USER_INFORMATION"        cursor.execute(sql)        results = ','.join([item for row in [list(i) for i in (cursor.fetchall())] for item in row]) # creates the comma separated list from my query        webbrowser.open('mailto: ' + results, new=1)对于 Windows Outlook,您只需将 ',' 替换为 ';' 因为电子邮件地址是分开的。我现在的最后一步是我只需要弄清楚如何识别用户使用的是 MacOS 还是 Windows。
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

如果您真的需要打开 Outlook,也许这将是 Open Outlook with Python的副本


os.startfile("outlook")

对于 macOS:


        subprocess.call(["open", filename])

另一方面,如果只是发送电子邮件有帮助,请检查 smtplib。


要开始一条空白消息,请使用以下命令:


import webbrowser

webbrowser.open('mailto:', new=1)

要编写您的电子邮件,您可以使用 smtplib(如上所述)+ 检查: https ://realpython.com/python-send-email/#sending-a-plain-text-email


步骤1 :


import smtplib, ssl


port = 465  # For SSL

password = input("Type your password and press enter: ")


# Create a secure SSL context

context = ssl.create_default_context()


with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:


server.login("my@gmail.com", password)

# TODO: Send email here

第2步 :


server.sendmail(sender_email, receiver_email, message)


sender_email = "my@gmail.com"

receiver_email = "your@gmail.com"

message = """\

Subject: Hi there


This message is sent from Python."""


# Send email here


查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 162 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号