Skip to content

Commit

Permalink
Merge pull request #117 from gopal-y/master
Browse files Browse the repository at this point in the history
Added send_mail_attachment() activity
  • Loading branch information
tvturnhout committed Apr 8, 2020
2 parents bd2d429 + 0715fcb commit 6a5f5dc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ Process | Description
<img src="https://github.com/OakwoodAI/automagica/blob/master/images/icons/salesforce.svg" width="20"> [Salesforce API](https://automagica.readthedocs.io/activities.html#automagica.activities.salesforce_api_call) | Activity to make calls to Salesforce REST API.
**E-mail (SMTP)** | ‌‌
<img src="https://github.com/OakwoodAI/automagica/blob/master/images/icons/mail-bulk-solid.svg" width="20"> [Mail with SMTP](https://automagica.readthedocs.io/activities.html#automagica.activities.send_mail_smtp) | This function lets you send emails with an e-mail address.
[Mail with attachment]() | This function lets you send emails with an e-mail address with files attached as attachments.
**Windows OS** | ‌‌
<img src="https://github.com/OakwoodAI/automagica/blob/master/images/icons/readme.svg" width="20"> [Find window with specific title](https://automagica.readthedocs.io/activities.html#automagica.activities.find_window_title) | Find a specific window based on the name, either a perfect match or a partial match.
<img src="https://github.com/OakwoodAI/automagica/blob/master/images/icons/passport-solid.svg" width="20"> [Login to Windows Remote Desktop](https://automagica.readthedocs.io/activities.html#automagica.activities.start_remote_desktop) | Create a RDP and login to Windows Remote Desktop
Expand Down
69 changes: 69 additions & 0 deletions automagica/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5980,6 +5980,75 @@ def send_mail_smtp(
smtpObj.sendmail(user, destination, BODY)
smtpObj.quit()

"""
E-mail (with attachments)
"""
@activity
def send_mail_attachment(_host, _user, _password, _to_address, _subject="", _message="", _port=587, _attachment=None):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
"""Mail using SMTP with attachments
This function lets you send emails with an e-mail address and also add attachments.
:parameter _host: The host of your e-mail account.
:parameter _user: The password of your e-mail account
:parameter _password: The password of your e-mail account
:parameter _to_address: The destination is the receiving mail address.
:parameter _subject: The subject
:parameter _message: The body of the mail
:parameter _port: The port variable is standard 587. In most cases this argument can be ignored, but in some cases it needs to be changed to 465.
:parameter _attachment: The attachments to be sent with the email are to be mentioned in the form of a dictionary
sample:
attachments={
'<filename1>.<extension>':'<filepath>',
'<filename2>.<extension>:'<filepath>'
}
:Example:
>>>attachments = {
'data.xlsx':'C:/Users/robot/Documents/data.xlsx',
'resume.pdf':'C:/Users/admin/Documents/resume.pdf'
}
>>> send_mail_smpt('robot@automagica.com', 'SampleUser', 'SamplePassword', 'robotfriend@automagica.com',attachment=attachments)
Keywords
mail, e-mail, email smpt, email attachments
Icon
las la-mail-bulk
"""
msg = MIMEMultipart() # storing the senders email address
msg['From'] = _user # storing the receivers email address
msg['To'] = _to_address # storing the subject
msg['Subject'] = _subject # string to store the body of the mail
body = _message # attach the body with the msg instance
msg.attach(MIMEText(body, 'plain')) # open the file to be sent

if _attachment!=None:
if 'dict' not in str(type(_attachment)):
raise TypeError("send_mail_attachment() expects a <class 'dict'> obj for attachments but %s"%str(type(l)))
else:
for i,j in _attachment.items():
filename = i
attachment = open(j, "rb")
p = MIMEBase('application', 'octet-stream') # instance of MIMEBase and named as p
p.set_payload((attachment).read()) # To change the payload into encoded form
encoders.encode_base64(p) # encode into base64
p.add_header('Content-Disposition', "attachment;filename= %s" % filename)# attach the instance 'p' to instance 'msg'
msg.attach(p)
smtpObj = smtplib.SMTP(_host, _port)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(_user, _password)
text = msg.as_string()
smtpObj.sendmail(_user, _to_address, text)
smtpObj.quit()

"""
Windows OS
Expand Down
1 change: 1 addition & 0 deletions docs/source/activities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Salesforce
E-mail (SMTP)
-------------
.. autofunction:: send_mail_smtp
.. autofunction:: send_mail_attachment


Windows OS
Expand Down

0 comments on commit 6a5f5dc

Please sign in to comment.