Sending emails

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:

2 Likes

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 Likes

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!

3 Likes

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

3 Likes

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:

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.

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.

1 Like

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.

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.

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:

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.

5 Likes

Afterwards just build a log that reads it all in PowerBI

7 Likes

good stuff!

I have taken this data tracking on-board as I am needing to do some reporting in the coming weeks.
I have made a quick python code Id like to share of my workflow.
This creates a blank text file of a timestamp, for each user of a machine, in a specified local or network location.
(As long as users have write access there)
Just swap out the @INSERT FOLDER HERE@ with your location. :grinning:

# Original Script by EwanO 2018 (Sastrugi Package) 
# Use as you like :-)
# This code creates a blank text file in a specified location that
# contains generic data.

import clr
import System
from System.IO import Directory

import sys
# load python
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

from datetime import datetime

# get the current date and time
datestamp = datetime.today().strftime("%Y-%m-%d")
timestamp = datetime.now().strftime("%H-%M-%S")

import os

## set the output directory and gather log data
main_dir = '@INSERT FOLDER HERE@'
user = os.environ.get('USERNAME')
computer = os.environ.get('COMPUTERNAME')
dir = main_dir + '\\' + computer + '\\' + user

# create a directory for each user of a machine
Directory.CreateDirectory(dir)

# generate the file name for the log file
filename = (str(datestamp) + '_' + str(timestamp) + '.txt')

# create the log file (blank text file)
logger = open(dir + '\\' + filename,'w+')

OUT = str("Run Logged")
8 Likes

wow this is cool. Would it be possible to Just save a text file to a specified location with the name format “date/time_user_project_script run”? :slight_smile:.

1 Like

I’ll have a look at this later on when I’m back at the PC. Had some ideas about variations to dynamically pick the storage method for usage information.

I’m importing peoples code all over the show, managed to get user and job number in there. Just cant get dynamo file name as its calling the whole directory i think. Pretty fun tho
image

3 Likes

@vanman
depending on dynamo version, there are different ways to get dynamo file name. Please see the below thread.

1 Like

@Timon

Hi Timon, this doesnt look like python code. What program was this made in? Can I use it just like this in Dynamo?

@mix There’s actually a much more robust version of this, I posted in this GitHub: https://github.com/Amoursol/dynamoPython/blob/master/dynamoAPI/dynamologger.py

It is python and can be used directly inside of dynamo.
Make sure and update line 58. to extract to your network folder.

The visualization show above is using Power BI. If anyone wants a copy of that reply to this message.

3 Likes