Untitled

 avatar
unknown
python
a year ago
1.0 kB
8
Indexable
def Email_send_function(to, subject, message, uname, pasw, html=False):
    try:
        # Create a secure SSL context
        with smtplib.SMTP('smtp.office365.com', 587) as server:
            server.starttls()
            server.login(uname, pasw)
            
            # Create a MIME multipart message
            msg = MIMEMultipart("alternative")
            msg['From'] = uname
            msg['To'] = to
            msg['Subject'] = subject

            # Attach message parts
            if html:
                msg.attach(MIMEText(message, 'html'))
            else:
                msg.attach(MIMEText(message, 'plain'))
            
            # Send the email
            server.sendmail(uname, to, msg.as_string())
            
            # Check if the server responded positively
            x = server.ehlo()
            if x[0] == 250:
                return "s"
            else:
                return "f"
    except Exception as e:
        print(f"Error: {e}")
        return "f"
Editor is loading...
Leave a Comment