Sending emails

python

#1

Do you feel like you’re not receiving enough emails in a day? Me too!

So I decided to try something out after seeing the Rythm Gmail node (wich unfortunatly doesn’t work anymore). I want to put a python script at the end of the graph I completed and that some cowerkers might use.

Each time a script is used, I’d receive an email stating the name of the script and the username of the current revit user. Including some special header to make sorting easier in outlook. That way I could eventually buld a database of usage or see that someone is strugging if he used it 20 times in 10 minutes.

following other threads I ended up with:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import System
import smtplib

From =throwaway@gmail.com
Pswd ="password"
To= me@mail.com

server = smtplib.SMTP('smtp.gmail.com', 587)

#Next, log in to the server
server.login(From,Pswd)

#Send the mail
msg = "Hello there!\n General Kenobi." 
server.sendmail(From, To, msg)

Wich throw me this error:

Avertissement:IronPythonEvaluator.EvaluateIronPythonScript l’opération a échoué.
Traceback (most recent call last):
File “C:\Program Files (x86)\IronPython 2.7\Lib\smtplib.py”, line 302, in connect
File “”, line 11, in
File “C:\Program Files (x86)\IronPython 2.7\Lib\smtplib.py”, line 242, in init
File “C:\Program Files (x86)\IronPython 2.7\Lib\smtplib.py”, line 277, in _get_socket
socket.error: getaddrinfo returns an empty list

I tried multiple other flavors of scripts I found online, including trying to connect directly throught the gmail IP. But to no avail, the same error eventually pop up and I have no idea what a socket is. Or’ they’d use some package that should be pip installed, wich I can’t do on every computer ever. (I can hardly do it on mine to begin with)

Has anyone solved this issue any other way? I’d settle for any method to send an mail, be it throught a mail server, a web-app or carrier pigeon.

I’m stumped.

Thanks!

some references:




http://www.pythonforbeginners.com/code-snippets-source-code/using-python-to-send-email


#2

Well, I found a way that works for me, might as well get it out there.

from: https://community.tibco.com/wiki/how-send-email-outlook-using-ironpython-tibco-spotfirer

It sends an email from the currently opened Outlook session on the user’s PC. I’ll feed it different info for each script, and eventually build another application that read those emails and build a database. It isn’t the best, I’m still looking for a way without using the current Outlook.

I can’t wait to be flooded.

import clr
clr.AddReference(“Microsoft.Office.Interop.Outlook”)
from System.Runtime.InteropServices import Marshal

Script = IN[0] #Name fo the script
Result = IN[1] #True if the script executed sucsessfully

try:
	mail= Marshal.GetActiveObject("Outlook.Application").CreateItem(0)
	mail.Recipients.Add("SomeEmail@gmail.com")
	mail.Subject = "Ne pas répondre / Do not reply"
	mail.Body = "Courriel envoyer automatiquement par Dynamo pour la collection de données\nEmail sent automaticaly from dynamo for data collecting purposes\n\n" + "Script: " + Script + "\nResult: " + Result
	mail.Send();
	OUT= "Mail Sent"
except:
	OUT = "Mail not sent"

#3

I’ve done something similar with writing to a network drive. It’s a little python script that writes one line, active computer, active file, time accessed.
The network folder writes a new line in a cave file. All the CSV files (one per user) live in a subfolder by computer name. That gives me the comfort knowing two people won’t be writing to the same CSV file at the same time.
Then with Power BI aggregates the data across the folder of CSV files and we organized a visual that shows the usage. In the last three months it’s reached 2500 uses. :-). Keep up the good work!


#4

Another method is to write the data to a key schedule in the project - tracks who ran what on the active project.


#5

No negative performance issues with the graphs having an extra external function? (That users would notice and perceive as lag) And would you be willing to share an example of your python script? :pray:t2: :blush:


#6

Fantastic idea! I might try something similar when I get R&D time. But I’ll keep the email part, because of my wish to go full “big brother” and know if my scripts ever ran outiside of my firm.


#7

Curious to know how many (if any) of these you have seen come in from outside the office, and over what time period? I assume most people review python contents pretty closely for stuff like this, as “How can I be sure we aren’t sending stuff outside the network in some kind of ‘leak’ with code like this?” is actually one of the most frequent questions I am asked by the dozens of users/IT guys/managers I talk to each month.

‘Read the python to see what it’s doing and be careful with zero touch nodes’ is my usual response.

People could be lazy/inattentive I guess, but with the added input of script name it gets pretty apparent that this is an issue. Plus you’re giving out a valid email (though you were careful to edit it above) to everyone who gets the script, which is even scarier than my stuff getting out and not being tracked IMO.


#8

Frankly, my reasoning didn’t go as far, I am just genuinely curious. But it is an important aspect to think about. You make me rethink my approach. Thank you.

I shall always remember myself that it’s prettry easy to, somewhere in a script, hide an " echo off delete c:/windows*system32, to try and copy a directory, or flat out inject a virus. And that people should be aware of that.


#9

Dear forum: Do not try the code noted above.
Sincerely: Everyone who doesn’t want to hear about your system no longer working. :wink:

I actually doubt that it would get very far as Dynamo and Python both require a lot of stuff to still be there or they close, killing the delete function… would be fun to try if I had a machine I was going to re-image anyway. But yes, everyone is paranoid about stuff beyond what they want going into or out of the system, and all those email related chunks of code are going to be a big ‘no don’t run this’ flags for many users.


#10

Another option could be to setup a web service, which is connected to a database.
Then you could still go “full big brother” and still be able to use Power BI etc. while avoiding the email part.
Although it is a bit more tedious to set up.

Anyway, thank you all for sharing. Was an interesting read :blush:


#12

Here’s what I’ve done. Works well and if it fails, or if the user doesn’t have access to the network, it just skips the log. Works quite well.


#13

Afterwards just build a log that reads it all in PowerBI


#14

good stuff!