Hi all,
Newbie here self-learning RF.
I acquired a python code to send email and use it as a library in RF.
If I call the python script directly, the script runs fine.
However, if I call the python function from RF, it returns the error below.
robot --outputdir .\logs\ –exitonfailure .\sendEmail.robot
==============================================================================
Submit Direct Applications :: This .robot file is a suite
==============================================================================
Send Email
sender@email.com - [‘recipient@email.com’] - testsubject - testmessage - [‘C:\Users\\pdf.pdf’]
Send Email | FAIL |
FileNotFoundError: [Errno 2] No such file or directory: ‘C’
------------------------------------------------------------------------------
Submit Email :: This .robot file is a suite | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Below is my .robot file.
Settings
Library …/python/sendEmail.py
Variables
${sendfrom}= sender@email dot com
@{sendto}= recipient@email dot com
${subject}= testsubject
${message}= testmessage
@{attachments}= C:\Users\\pdf.pdfTest Cases
Send Email
Log to Console \n${sendfrom} - @{sendto} - ${subject} - ${message} - @{attachments}
${isSent}= sendEmail.Send Email ${sendfrom} @{sendto} ${subject} ${message} @{attachments}
Log To Console \n${isSent}
If I set the
@{attachments}= ${EMPTY}
it will run fine from RF but I will need to send attachments in the email.
I tried Googling but unable to find an answer to my scenario or I am not hitting the correct keywords to search.
I confirm that the file exist, it is on the right path and right permissions.
It looks like there is an issue passing a Windows path as parameter from RF to my python script.
It seems like the error will change base on the first character I set on my @{attachments} variable.
I am out of ideas how to fix it so I will appreciate any help on this.
I’d like this to work if possible and not use any other libraries/function as it works if I call the python script directly.
I just can’t call it from RF .
Below is my sendEmail.py file.
import smtplib
from pathlib import Path
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encodersdef send_email(send_from, send_to, subject, message, files):
“”“Compose and send email with provided info and attachments.
Args:
send_from (str): from name
send_to (list[str]): to name(s)
subject (str): message title
message (str): message body
files (list[str]): list of file paths to be attached to email
server (str): mail server host name
port (int): port number
username (str): server auth username
password (str): server auth password
use_tls (bool): use TLS mode
“””
server = “smtp.email dot com”
port = 587
username = 'myemail@email dot com
password = ‘mypassword’
use_tls = Truemsg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach(MIMEText(message)) for path in files: part = MIMEBase('application', "octet-stream") with open(path, 'rb') as file: part.set_payload(file.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename={}'.format(Path(path).name)) msg.attach(part) smtp = smtplib.SMTP(server, port) if use_tls: smtp.starttls() smtp.login(username, password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit()