Use smtplib to send emails in python, only the first mailbox receives the solution

1. Problem description

        When using the smtplib library in python for mass mailing operations, only the first email address has received the email, and the rest of the email addresses have not received the email.

For example:

Recipient = [email protected], [email protected], [email protected], only [email protected] received the email.

2. The cause of the problem

        The data type of MIMEText () [ 'To' ] and the data type of to_addrs in sendmail () are wrong and cause this problem. The values ​​in these two places should be set to fixed data types, the value of MIMEText () ['To'] should be of string type, and the value of to_addrs in sendmail() should be of list type.

        In the writing of the code, the format requirements are met by means of type conversion and other means to realize the mass mailing function. The following are two writing methods, one is to write the code with fixed parameters, and the other is to write the code by calling the configuration file. Take qq mailbox as an example.

3. Writing method 1 (write the mailbox into the running code)

 

4. Writing method 2 (obtain mailbox through configuration file)

1. Write a configuration file named conf.ini, and write the information to be changed, such as sender and recipient, into a configurable file.

 

2. Write a read_conf.py file, encapsulate the read configuration file into a function, and the function name is email_info.

Special note: conf.items( 'email' ) will get all the options under email in the ini file. The default is list format, and each set of data is a tuple. It is not easy to operate later, so it is converted to dict(conf .items( 'email' )) output as a dictionary. 

 3. Write

 V. Summary

        Regardless of the writing method, just ensure that the value of MIMEText () [ 'To' ] should be of string type, and the value of to_addrs in sendmail() should be of list type.

Guess you like

Origin blog.csdn.net/v781423070/article/details/128747108