A2N15
A2N15

Reputation: 605

How to schedule a Python script to send mail while user not logged?

I'm trying to automate a script to send an email with python.

Below is the script that works (tried on cmd prompt) "python_send_mail.py"

import numpy as np
import pandas as pd
import win32com.client as win32

df = pd.DataFrame({'a' : [1, 2, 3]})
df.to_csv('C:\\Users\\A2N\\Desktop\\Python_Scheduler\\data_mail_new.csv', index = False)

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'Robot Mail'
mail.HtmlBody = 'Hello buddy, long time no see.'
attachment = 'C:\\Users\\A2N\\Desktop\\Python_Scheduler\\data_mail_new.csv'
mail.Attachments.Add(Source = attachment)
mail.Send()

Thanks to the help of the Stack Community, I've been able to schedule it and run it when the user is logged on. Below are the information provided

Program Script: %SystemRoot%\System32\cmd.exe
Add Arguments: /D /C ("C:\Users\A2N\Anaconda3\Scripts\activate.bat" & set & "C:\Users\A2N\Anaconda3\python.exe" "C:\Users\A2N\Desktop\Python_Scheduler\python_send_mail.py") > "C:\Users\A2N\Desktop\Python_Scheduler\Log_win32_mail.txt" 2>&1

However, when I try to run it "whether the user is logged on or not", the task is running without giving any output.

Things tried :

There is no error on the log file that I create, just the var envs.

So I'm wondering, if there is something that should be done on the computer settings?

Upvotes: 0

Views: 1842

Answers (2)

jonathanfuchs
jonathanfuchs

Reputation: 69

If the goal is "to automate a script to send an email with python," and you don't need to use Outlook for this for some unstated reason, I'd recommend using the smtplib library that comes with python instead. For one, this avoids the issue Eugene brings up. For another, a script using the smtplib will work whether the user is logged on or not, as the login credentials would be stored in the script and used each time an email is sent.

For the below to work the email service your Outlook is connecting to needs to have smtp enabled.

import numpy as np
import pandas as pd
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

df = pd.DataFrame({'a' : [1, 2, 3]})
df.to_csv('C:\\Users\\A2N\\Desktop\\Python_Scheduler\\data_mail_new.csv', index = False)

msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'Robot Mail'
body = 'Hello buddy, long time no see.'
msg.attach(MIMEText(body, 'plain'))
attachment=open('C:\\Users\\A2N\\Desktop\\Python_Scheduler\\data_mail_new.csv','rb').read()
x = MIMEBase('application', 'octet-stream')
x.set_payload(attachment)
encoders.encode_base64(x)
x.add_header('Content-Disposition', 'attachment', filename='data_mail_new.csv')
msg.attach(x)

# The port_number and server address below should be changed to your SMTP server details, 
# and the login credentials should be changed as well
port_number = 1025
mailserver = smtplib.SMTP('localhost',port_number)
mailserver.login("[email protected]", "PASSWORD1234")
mailserver.sendmail('[email protected]','[email protected]',msg.as_string())
mailserver.quit()  

This script can then be safely automated with the Task Scheduler

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

Automating Outlook from a task scheduler is not really a good idea. The Considerations for server-side Automation of Office article states the following:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

You may consider using Graph API or EWS instead.

Upvotes: 0

Related Questions