Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Sending email from custom domain

Tags:

python

email

smtp

I am trying to send an email from a custom domain in Python. I have figured out how to send emails from other domains, like gmail.com, using smtplib [example code]. Now I want to figure out how to send an email from a custom domain like catsareonwheels.com.

I thought I would be able to use smtpd to send emails from a server, but that library appears only to serve as a proxy fora Mail Transfer Agent. From what I can tell, there are MTA's written in Pure Python [e.g. Slimta], but I have not been able to find any examples of snippets that actually demonstrate how to send an email from a custom domain with Python.

If anyone can help point me toward literature that might help me determine how best to achieve this goal, I'd be very grateful.

like image 880
duhaime Avatar asked Jun 28 '26 01:06

duhaime


1 Answers

TO SEND AN EMAIL FROM A CUSTOM DOMAIN, YOU DO THE FOLLOWING:

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

msg = MIMEMultipart()
msg["From"] = "<your_mail_account>"
msg["To"] = "<destiny_account>"
body_text = "HOLA MUNDO :)"
body_part = MIMEText(body_text, 'plain')
msg.attach(body_part)
with smtplib.SMTP(host="smtp.<CUSTOM_DOMAIN>.com", port=587) as smtp_obj:  # ENVIAR DESDE UN DOMINIO PERSONALIZADO.
    smtp_obj.ehlo()
    smtp_obj.starttls()
    smtp_obj.ehlo()
    smtp_obj.login("<your_mail_account>", "<your_password>")
    smtp_obj.sendmail(msg['From'], [msg['To'],], msg.as_string())

print("¡Datos enviados con éxito!")

That's all :) Hope you like it!

like image 92
DANIEL ROSAS PEREZ Avatar answered Jun 30 '26 19:06

DANIEL ROSAS PEREZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!