YouTip LogoYouTip

Python3 Smtp

## Python3.x Python3 SMTP Send Email SMTP (Simple Mail Transfer Protocol) is a set of rules for transferring mail from source address to destination address, which controls the way messages are forwarded. Python's smtplib provides a convenient way to send emails. It is a simple wrapper around the SMTP protocol. The syntax to create an SMTP object in Python is as follows: import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) Parameter description: * host: SMTP server host. You can specify the host's IP address or domain name like , this is an optional parameter. * port: If you provide the host parameter, you need to specify the port number used by the SMTP service. Generally, the SMTP port number is 25. * local_hostname: If SMTP is on your local machine, you only need to specify the server address as localhost. Python SMTP object uses the sendmail method to send emails, with the following syntax: SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options] Parameter description: * from_addr: Email sender address. * to_addrs: String list, email recipient addresses. * msg: Message to send Note about the third parameter, msg is a string representing the email. As we know, emails are generally composed of subject, sender, recipient, content, attachments, etc. When sending emails, pay attention to the format of msg. This format is defined in the SMTP protocol. ### Example The following is a simple example of sending an email using Python: ## Example import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@'receivers = ['429240967@qq.com']message = MIMEText('Python Email Sending Test...', 'plain', 'utf-8')message['From'] = Header("", 'utf-8')message['To'] = Header("Test", 'utf-8')subject = 'Python SMTP Email Test'message['Subject'] = Header(subject, 'utf-8')try: smtpObj = smtplib.SMTP('localhost')smtpObj.sendmail(sender, receivers, message.as_string())print("Email Sent Successfully")except smtplib.SMTPException: print("Error: Unable to Send Email") We use three quotes to set up the email information. A standard email requires three headers: **From**, **To**, and **Subject**, each separated by a blank line. We connect to the SMTP server by instantiating the SMTP object _smtpObj_ of the smtplib module, and use the _sendmail_ method to send the message. Execute the above program, if sendmail is installed on your machine, it will output: $ python3 test.py Email Sent Successfully Check your inbox (usually in the spam folder), and you can see the email: !(#) If we don't have sendmail on our local machine, we can also use other service providers' SMTP access (QQ, NetEase, Google, etc.). ## Example import smtplib from email.mime.text import MIMEText from email.header import Header mail_host="smtp.XXX.com"mail_user="XXXX"mail_pass="XXXXXX"sender = 'from@'receivers = ['429240967@qq.com']message = MIMEText('Python Email Sending Test...', 'plain', 'utf-8')message['From'] = Header("", 'utf-8')message['To'] = Header("Test", 'utf-8')subject = 'Python SMTP Email Test'message['Subject'] = Header(subject
← Python3 Xml ProcessingPython Socket β†’