Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send mail in Python on linux server via "mail"?

I'm trying to send mail on an event trigger from python on a linux server. The server already has mail installed and I have my code like so:

import subprocess
subprocess.call(["mail","-s Hello there", "[email protected] < 
this_text_document.txt"])

Feel as if I am blind as a bat here. Receive perpetual loading. Upon interrupt receive this: "No recipients specified ... message not sent" Please send help.

like image 810
Noah Hampton Avatar asked Feb 04 '26 06:02

Noah Hampton


1 Answers

this will work:

def SendMail(Subject: str, Body: str):
    body_str_encoded_to_byte = Body.encode()
    return_stat = subprocess.run([f"mail", f"-s {Subject}", "[email protected]"], input=body_str_encoded_to_byte)
    print(return_stat) 
like image 173
idan357 Avatar answered Feb 05 '26 23:02

idan357