I want to send a computer-generated email by my work email address using a python script. All of the tutorials I found were about Gmail account.
You can send pdf file and html using gmail smtplib
1- Allow less secure apps in gmail apps
2- use smtplib to send an email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.message import EmailMessage
from email.mime.base import MIMEBase
from email import encoders
filename = "filename.pdf"
receivers = ["receiver@gmail.com"]
subject= "Email subject"
server = smtplib.SMTP('smtp.gmail.com', 587)
message = MIMEMultipart()
message["From"] = "Me"
message['To'] = '_'.join(receivers)
message["Subject"] = subject
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
html = """\
<p>
HTML data
</P>
"""
message.attach(part)
message.attach(MIMEText(html, "html"))
message = message.as_string()
# start TLS for security user_to
server.starttls()
# Authentication
server.login("me@gmail.com", "password")
sender = "email"
try:
server.sendmail(sender, receivers, message)
except Exception as e:
raise e