How to send emails via outlook using terminal sever - python-3.x

I have a python script which is used to send email through outlook, but the script has got my system's path,
now I have to run this script on centralised system using putty, as the code is now on git, how do I change the path for 'mail.Attachments' section.
Below is the script which runs fine on my local machine, but when i try to run it on putty it throws error, module win32com.client not found and doesn't even allow me to install pywin32
import win32com.client
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.createItem(0)
mail.To = 'xsupport#xsample.com '
mail.Subject = 'certificate CSR'
mail.Body = "Attached is the CSR for 'xxx.sample.com'.\
\nPlease request a duplicate certificate for the Cert Project.\
\n\nThanks,\nPraveen"
mail.Attachments.Add('C:/Users/praveen23/akamai_cert/sample.pem')
mail.Send()

Related

Winidea Python script called from Jenkins

I am trying to launch Winidea configuration stored on U: drive. I am able to execute this script using command promt. However when I am trying to execute this script using Jenkins, it is giving error related to dll. My code is
import isystem.connect as ic
import time
print('isystem.connect version: ' + ic.getModuleVersion())
# 1. connect to winIDEA Application
pathTowinIDEA = 'C:/winIDEA/iConnect.dll'
cmgr_APPL = ic.ConnectionMgr(pathTowinIDEA)
cmgr_APPL.connectMRU('U:/winIDEA/myconfig.xjrf')
debug_APPL = ic.CDebugFacade(cmgr_APPL)
ec = ic.CExecutionController(cmgr_APPL)
The error is coming at line
cmgr_APPL.connectMRU('U:/winIDEA/myconfig.xjrf')
Error is as follow
enter image description here

Save a text file to a remote file server using python production code

I have to save a text file to a remote file server instead of my local system. I am using python3. Initially, I have looked up this and found subprocess method to do it. However, I want to know from the experts which is the best way to do the same(saving a text file in a remote file server) in production.
Any help is appreciated.
You can call the scp bash command (it copies files over SSH) with subprocess.run:
import subprocess
subprocess.run(["scp", FILE, "USER#SERVER:PATH"])
Or try the Python Paramiko. It's very easy to use:
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, password=password)
sftp = ssh.open_sftp()
sftp.put(localpath, remotepath)
sftp.close()
ssh.close()

Send email script works on Win, but not on Android

This script sends mail when is ran on Windows and Linux, but not on Andorid. What is the issue?
My app requirements in p-4-a are python3, kivy, openssl. Permission is INTERNET. App crashes when I run method below.
def send_report(self):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('sender_email', 'sender_pass')
server.sendmail('sender_email', 'receiver_email', 'Some msg')
server.close()

Connecting to FTPS with Python

I am trying to connect to an FTPS server which requires anonymous login with a .pfx certificate.
I have been given instructions for how to access it through the gui application SmartFTP which does work, thus I know I haven't got any firewall issues etc. However, for this workflow getting access to it through python would be ideal. Below are the settings I have been given:
Protocol: FTPS (Explicit)
Host: xxx.xxx.xxx.xxx
Port: 21
login type: Anonymous
Client Certificate: Enabled (providing a .pfx file)
Send FEAT: Send before and after login
I am having trouble picking the python module best suited to this with a full example using a .pfx certificate. Currently I have only tried the standard FTP module using the below code. Does anyone have a worked example?
from ftplib import FTP_TLS
ftps = FTP_TLS(host='xxx.xxx.xxx.xxx',
keyfile=r"/path/to.pfx"
)
ftps.login()
ftps.prot_p()
ftps.retrlines('LIST')
ftps.quit()
Using the above code I get:
ValueError: certfile must be specified
Client versions:
Ubuntu == 14.04,
Python == 3.6.2
Update
Think I am a little closer with the code below but getting a new error:
from ftplib import FTP_TLS
import tempfile
import OpenSSL.crypto
def pfx_to_pem(pfx_path, pfx_password):
""" Decrypts the .pfx file to be used with requests. """
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
pfx = open(pfx_path, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
ca = p12.get_ca_certificates()
if ca is not None:
for cert in ca:
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))
f_pem.close()
yield t_pem.name
pfx = pfx_to_pem(r"/path/to.pfx", 'password')
ftps = FTP_TLS(host='xxx.xxx.xxx.xxx',
context=pfx
)
ftps.login()
ftps.prot_p()
# ftps.prot_c()
print(ftps.retrlines('LIST'))
ftps.quit()
Error:
ftplib.error_perm: 534 Local policy on server does not allow TLS secure connections.
Any Ideas?
Cheers
It sounds like you try to do SFTP. FTP over SSL is not the same as SFTP. As far as I know SFTP (which is related to SSH) is not possible with the standard library.
See this for more about SFTP in Python: SFTP in Python? (platform independent)

Send an email from a Python3 script with localhost?

I need to send mails from my Python3 script. Now it does, but my gmail password is visible and I cannot trust in any admin of this machine, so the solution I see is to mount a local mail server. To do some tests, I was trying to execute a script (this one: SMTP sink server). While this one is running, I execute my old script with some changes:
import smtplib
# server = smtplib.SMTP('smtp.gmail.com:587')
server = smtplib.SMTP('localhost:25')
# smtp.ehlo()
# server.starttls()
# smtp.ehlo()
# server.login('my_account#gmail.com', 'my_password')
server.login(None, None)
server.sendmail('Me <my_account#gmail.com'>, ['to_user#gmail.com'], 'Hi!'.as_string())
server.quit()
I understand the script at the link will create a file in the folder where it is with the mail content, but nothing happens, because I get this error message:
SMTP AUTH extension not supported by server.
I googled and I think this could be sorted out if I uncomment the line server.starttls(), but it gives another error, which is supposed to be solved with the lines smtp.ehlo(), but not in my case.
Any suggestions?
OK, I managed to send the email, what I only had to do was removing this line:
server.login(None, None)

Resources