Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

smtplib doesn't send an email | Python

Not getting errors but it's not sending an email to the receivers email neither.

Using an email I got from namecheap (they use privateemail).

async def contents(self, email): 
    email = email.strip()
    message = MIMEMultipart()
    message["From"] = "[email protected]"
    message["To"] = email
    message["Subject"] ="SCARY HVH CONFIGURATION"
    message["Bcc"] = email
    message.attach(MIMEText(open("scarychina.txt", "r+").read(), "plain"))
    with open("js.zip", '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= js.zip",)
    message.attach(part)
    return str(message)

async def send_config(self, email: str):
    try:
        contents = await self.contents(email)
        server = smtplib.SMTP_SSL("mail.privateemail.com", 465, ssl.create_default_context())
        server.login("[email protected]", email_pass)
        server.sendmail("[email protected]", email, contents)
        print("Sent an email to " + email)
        server.quit() 
    except Exception as e:
        print(e)```
question from:https://stackoverflow.com/questions/65839153/smtplib-doesnt-send-an-email-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

My email is feasible, the code is as follows

import smtplib 
from email.mime.text import MIMEText
from email.utils import formataddr


class SendMail(object):
    def __init__(self, mail_title, mail_contain):
        self.sender='sender email'
        self.accepter='accepter email'
        self.mail_title = mail_title
        self.mail_contain = mail_contain

    def send_data(self):
        try:
            msg = MIMEText(self.mail_contain, 'html', 'utf-8')
            msg['From'] = formataddr(["sender name", self.sender])
            msg['To'] = ";".join(self.accepter)
            msg['Subject'] = self.mail_title
            server = smtplib.SMTP_SSL("smtp host",465)
            server.login(self.sender, "password")
            server.sendmail(self.sender, self.accepter, msg.as_string())
            server.quit()
        except Exception as e:
            print(e, 'send fail....')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...