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

我正在尝试启动本地邮件应用程序、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。


POPMUISE
浏览 163回答 1
1回答

牛魔王的故事

如果您真的需要打开 Outlook,也许这将是 Open Outlook with Python的副本os.startfile("outlook")对于 macOS:        subprocess.call(["open", filename])另一方面,如果只是发送电子邮件有帮助,请检查 smtplib。要开始一条空白消息,请使用以下命令:import webbrowserwebbrowser.open('mailto:', new=1)要编写您的电子邮件,您可以使用 smtplib(如上所述)+ 检查: https ://realpython.com/python-send-email/#sending-a-plain-text-email步骤1 :import smtplib, sslport = 465  # For SSLpassword = input("Type your password and press enter: ")# Create a secure SSL contextcontext = 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 thereThis message is sent from Python."""# Send email here
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python