Python Forum
Sending email using own smtp server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending email using own smtp server
#1
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
Reply
#2
this looks like a good how to to follow

https://hackernoon.com/a-step-by-step-gu...ith-python
Reply
#3
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.
Reply
#4
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
)
robertkwild likes this post
Reply
#5
Port 587 will require TLS, I'm sure!
Reply
#6
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
Reply
#7
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
Reply
#8
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
Reply
#9
email = une.stdout.strip()
print(email)
robertkwild likes this post
Reply
#10
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 811 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  How to take the tar backup files form remote server to local server sivareddy 0 2,040 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  send email smtp rwahdan 0 1,890 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Sending Out Email via Python JoeDainton123 1 4,977 Aug-31-2020, 12:54 AM
Last Post: nilamo
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,984 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,478 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  Python sockets : Client sending to the server nico31780 0 2,499 May-17-2020, 07:56 PM
Last Post: nico31780
  SMTP problem MajK 6 3,362 May-09-2020, 07:47 AM
Last Post: MajK
  Sending an email with attachment without using SMTP? PythonNPC 5 3,401 May-05-2020, 07:58 AM
Last Post: PythonNPC
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,262 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020