Sending email using own smtp server

hi all,

ive made my own smtp server so in my python script i can use my smtp server to send emails

is there a good how to to follow please

thanks,
rob

There exists smtplib module.

Here is an example in it.

this looks like a good how to to follow

ok this works

import smtplib
from email.mime.text import MIMEText

smtp_server = '172.17.1.5'
smtp_user = 'robertkwild@domain.com'
smtp_connection = smtplib.SMTP(smtp_server)
email_body = 'This is a test email sent using Python.'
email_message = MIMEText(email_body)
email_message['Subject'] = 'Test Email'
email_message['From'] = 'robertkwild@domain.com'
email_message['To'] = 'robert.wild@domain.com'
smtp_connection.sendmail(smtp_user, 'robert.wild@domain.com', email_message.as_string())
smtp_connection.quit()

but as ive already stated the “to” address cant i use that in the “smtp_connection.sendmail”

thanks,
rob

EDIT - i deleted the “to” line and still works

this is throwing a syntax error, can you help me whats wrong with this command

une = subprocess.run(f'powershell.exe $cred = Import-CliXml -Path C:\\python\\cred.xml; Get-aduser -credential $cred -server {form.dom.data} -identity {form.un.data} -properties * | select-object -expandproperty mail)

basically i want to save that powershell command into variable “une” which is the usernames email address ie robert.wild@domain.com

thanks,
rob

you missed closing a single quote after “main” in the end.

1 Like

LOL as soon as i posted i worked it out

ok im trying to grab the email from powershell get-aduser and its worked but i get a whitespace with my result as well

how do i get rid of the white space please

une = subprocess.run(f'powershell.exe $cred = Import-CliXml -Path C:\\python\\cred.xml; Get-aduser -credential $cred -server {dom} -identity {un} -properties * | select-object -expandproperty mail', capture_output=True, text=True)
>>> print(une.stdout)
robertkwild@gmail.com

>>>

thanks,
rob

une.stdout.strip() will remove all whitespace at the beginning and end of the string.

1 Like

wow got it all to work, heres the end result

import smtplib
from email.mime.text import MIMEText
import subprocess

un = "robert.wild"
dom = "dc01.robo84.net"
cnp = "Password01!"

une = subprocess.run(f'powershell.exe $cred = Import-CliXml -Path C:\\python\\cred.xml; Get-aduser -credential $cred -server {dom} -identity {un} -properties * | select-object -expandproperty mail', capture_output=True, text=True, shell=False)

smtp_server = '172.17.1.5'
smtp_user = 'robertkwild@domain.com'
smtp_connection = smtplib.SMTP(smtp_server)
email_body = f'hello {un} your new password is {cnp} for {dom}'
email_message = MIMEText(email_body)
email_message['Subject'] = 'password change'
email_message['From'] = 'robertkwild@domain.com'
smtp_connection.sendmail(smtp_user, {une.stdout}, email_message.as_string())
smtp_connection.quit()
1 Like