import smtplib from email.mime.text import MIMEText from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.mime.image import MIMEImage from email.header import Header from email.utils import parseaddr, formataddr
server = smtplib.SMTP_SSL(smtp_server, 465) # SMTP SSL协议默认端口是465,注意使用SMTP_SSL server.set_debuglevel(3) try: server.login(from_addr, password) server.sendmail(from_addr, [to_addr], msg.as_string()) server.quit() except Exception as e: print(e)
MIME 类型
通用类型
1 2 3 4 5 6 7 8 9 10 11 12
class email.mime.application.MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, *, policy=compat32, **_params) Module: email.mime.application
A subclass of MIMENonMultipart, the MIMEApplication class is used to represent MIME message objects of major type application. _data is a string containing the raw byte data. Optional _subtype specifies the MIME subtype and defaults to octet-stream.
Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the data for transport. This callable takes one argument, which is the MIMEApplication instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.
Optional policy argument defaults to compat32.
_params are passed straight through to the base class constructor.
Changed in version 3.6: Added policy keyword-only parameter.
音频类型
1 2 3 4 5 6 7 8 9 10 11 12
class email.mime.audio.MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶ Module: email.mime.audio
A subclass of MIMENonMultipart, the MIMEAudio class is used to create MIME message objects of major type audio. _audiodata is a string containing the raw audio data. If this data can be decoded by the standard Python module sndhdr, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the audio subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, then TypeError is raised.
Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the audio data for transport. This callable takes one argument, which is the MIMEAudio instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.
Optional policy argument defaults to compat32.
_params are passed straight through to the base class constructor.
Changed in version 3.6: Added policy keyword-only parameter.
图片类型
1 2 3 4 5 6 7 8 9 10 11 12
class email.mime.image.MIMEImage(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params) Module: email.mime.image
A subclass of MIMENonMultipart, the MIMEImage class is used to create MIME message objects of major type image. _imagedata is a string containing the raw image data. If this data can be decoded by the standard Python module imghdr, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the image subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, then TypeError is raised.
Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the image data for transport. This callable takes one argument, which is the MIMEImage instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.
Optional policy argument defaults to compat32.
_params are passed straight through to the MIMEBase constructor.
Changed in version 3.6: Added policy keyword-only parameter.