Untitled

mail@pastecode.io avatar
unknown
python
15 days ago
1.0 kB
5
Indexable
Never
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"
Leave a Comment