简体   繁体   中英

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"] = "config@hvh.monster"
    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("config@hvh.monster", email_pass)
        server.sendmail("config@hvh.monster", email, contents)
        print("Sent an email to " + email)
        server.quit() 
    except Exception as e:
        print(e)```

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....')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM