python mailer code example
Example 1: email in python
import smtplib
from email.message import EmailMessage
EmailAdd = "Email id"
Pass = "Email Password"
msg = EmailMessage()
msg['Subject'] = 'Subject of the Email'
msg['From'] = EmailAdd
msg['To'] = '[email protected]','[email protected]'
msg.set_content('Mail Body')
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
smtp.login(EmailAdd,Pass)
smtp.send_message(msg)
Example 2: send email python
from mailer import Mailer
mail = Mailer(email='[email protected]', password='your_password')
mail.send(receiver='[email protected]', subject='TEST', message='From Python!')
Example 3: send mail from python
import smtplib
sender = '[email protected]'
receivers = ['[email protected]']
message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Example 4: malier module python
from mailer import Mailer
from mailer import Message
message = Message(From="[email protected]",
To="[email protected]",
charset="utf-8")
message.Subject = "An HTML Email"
message.Html = """This email uses <strong>HTML</strong>!"""
message.Body = """This is alternate text."""
sender = Mailer('smtp.example.com')
sender.send(message)
Example 5: email sender python
import smtplib
from email.message import EmailMessage
with open(textfile) as fp:
msg = EmailMessage()
msg.set_content(fp.read())
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()