0

I am attempting to launch the local mail application, Outlook 365 on windows and Mail on MacOS, and start a blank email, with the (To: ) column filled in from a database query.

Ideally it would look something similar to:

def email_create():
     if MacOS:
        open Mail.App
        To: = [sql statement]
     if Windows
        open Outlook.application
        To: = [sql statement]

EDIT: With the help of @spYder below, I wanted to just post my final product that seems to work great. Thank you again @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)

For windows outlook you would just replace the ',' with ';' because of the way email addresses are separated. My last step now is I just need to figure out how to identify if the user is using MacOS or Windows.

Knightman
  • 3
  • 4

1 Answers1

1

If you really need to open Outlook, Maybe this would be a duplicate of Opening Outlook with Python

os.startfile("outlook")

For macOS :

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

On the other hand, if just sending the email helps, check smtplib.

To start a blank message use the following:

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

To write your email you can use smtplib ( as mentioned above ) + check : https://realpython.com/python-send-email/#sending-a-plain-text-email

Step 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

Step 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
spYder
  • 317
  • 1
  • 2
  • 11
  • Once I have the outlook application open, how can I start a new email and populate the To: information? Also, would this work for MacOS, os.startfile("Mail") ? – Knightman Sep 19 '19 at 21:34
  • I believe writing the email would be another question ( because your post is about opening the Mail app ) but as I have mentioned, you can check smtplib. I will briefly edit my code above to help... – spYder Sep 19 '19 at 21:39
  • My question title is "Open mail program and start a blank email with python" opening the application is part 1, part 2 is start a blank email. Your above edit is regarding sending an email from the email server, I would like to just open a blank email so the user can fill in the content and send the email from their desktop application. Thank you for your assistance @spYder – Knightman Sep 19 '19 at 21:44
  • The behavior I'm looking for would be the same behavior you get from the html code 'Send Email' this would launch your mail application and start a blank email. – Knightman Sep 19 '19 at 21:48
  • Try subprocess.call(["xdg-open", filename]) for Mac – spYder Sep 19 '19 at 21:49
  • I got this to work for opening the application on MacOS. But now I need to launch a blank email. import subprocess subprocess.call( ["/usr/bin/open", "-a", "/Applications/Mail.app"] ) – Knightman Sep 19 '19 at 21:52
  • To launch a blank email you just use webbrowser... webbrowser.open('mailto:', new=1) – spYder Sep 19 '19 at 21:58
  • You rock. That worked perfectly. Thank you @spYder for all your help. – Knightman Sep 19 '19 at 22:33