Python Forum

Full Version: Sending email using own smtp server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
this looks like a good how to to follow

https://hackernoon.com/a-step-by-step-gu...ith-python
I think your emails will not get through.

Imagine the amount of spam we would have if everyone could just send emails from home, no checks!

The receiving server will look to see who is sending, and reject your unknown server request.

What you can do is use gmail, yahoo, QQ or other where you have an account

smtp_server = "smtp.gmail.com"
You need your email and an App password, get that from gmail or whoever, the app-password looks like abcd efgh ijkl mnop

But you can't send too many at one time, or the server will complain and end the connection. I sent 104 from QQ before QQ got angry!

Have a look here.

If you need help building the email, let me know.

If you want to send thousands of emails a day, there are companies which specialise in that.
Use yagmail the best Pyhon email client.
import yagmail

# Replace with your SMTP server details
smtp_host = 'smtp.yourserver.com'
smtp_port = 587  # or the port your server uses
smtp_user = 'yourusername'
smtp_password = 'yourpassword'

yag = yagmail.SMTP(
    user=smtp_user,
    password=smtp_password,
    host=smtp_host,
    port=smtp_port
)

# Sending an email
yag.send(
    to='[email protected]',
    subject='Subject',
    contents='This is the body of the email'
) 
Additional Configuration:
If your server requires SSL/TLS, you can configure that by specifying the smtp_starttls parameter.
yag = yagmail.SMTP(
    user=smtp_user,
    password=smtp_password,
    host=smtp_host,
    port=smtp_port,
    smtp_starttls=True,  # Set to True if your server uses TLS
    smtp_ssl=False  # Set to True if your server uses SSL
)
Port 587 will require TLS, I'm sure!
ok this works

import smtplib
from email.mime.text import MIMEText

smtp_server = '172.17.1.5'
smtp_user = '[email protected]'
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'] = '[email protected]'
email_message['To'] = '[email protected]'
smtp_connection.sendmail(smtp_user, '[email protected]', 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 [email protected]

thanks,
rob

LOL missed a ' at the end
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)
[email protected]

>>>
thanks,
rob
email = une.stdout.strip()
print(email)
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 = '[email protected]'
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'] = '[email protected]'
smtp_connection.sendmail(smtp_user, {une.stdout}, email_message.as_string())
smtp_connection.quit()
Pages: 1 2