Home > Net >  fail to use variable from python panda sending Gmail
fail to use variable from python panda sending Gmail

Time:06-13

I am layman for python. I want to write a code for sending email under certain condition that "Name" which is VIP in my data set The data set and filter worked fine, and function of sending email also worked but I don't know how to apply the variable e = email which associate with the value VIP=Yes and i = Name which associate with VIP value = Yes. I try to send a email to specific address by filling the name by variable i. but it became attachment with "no name". Can anyone help? Many Many thanks!

import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
df = pd.read_excel('s1.xlsx')
print('RAW_DATA')
print(df)
print('========================================')
df1 = df.loc[df.VIP.str.startswith('Y')]  #'Sowing the name associted with VIP by the vaule Y'
df0 = df.loc[df.VIP.str.startswith('N')]
df2 = pd.DataFrame(df1,columns=['Name','VIP','Email']) #display the information that relate to 'df1'
df00 = pd.DataFrame(df0, columns=['Name','VIP','Email'])
print('VIP LIST')
print (df2)
print('========================================')
print('Non-VIP LIST')
print (df00)
print('========================================')

content = MIMEMultipart()  
content["subject"] = "Learn Code With Mike" 
content["from"] = "*****@gmail.com"  #send from
for e in df2.Email:
    content["to"] = e # receiver
content.attach(MIMEText(i,"Demo python send email")) #content

import smtplib
with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:  
    try:
        smtp.ehlo()  # 
        smtp.starttls()  # 
        smtp.login("*****.com", "******")  # 
        smtp.send_message(content)  # 
            print("Complete!")
        except Exception as s:
            print("Error message: ", s)

Output 
RAW_DATA
    Name  VIP  Age                  Email
0  Felix  YES   32        [email protected]
1  Chris   NO   17        [email protected]
2    Tom  YES   26          [email protected]
3  Dover  YES   34     [email protected]
4    Tim  YES   32        [email protected]
5   Wing   NO   42       [email protected]
6  Peter   NO   35        [email protected]
7  Brown  YES   24    [email protected]
8   Fung  YES   54   [email protected]
9   Ryan   NO   37     [email protected]
========================================
VIP LIST
    Name  VIP                  Email
0  Felix  YES     [email protected]
2    Tom  YES          [email protected]
3  Dover  YES     [email protected]
4    Tim  YES        [email protected]
7  Brown  YES    [email protected]
8   Fung  YES   [email protected]
========================================
Non-VIP LIST
    Name VIP               Email
1  Chris  NO     [email protected]
5   Wing  NO    [email protected]
6  Peter  NO     [email protected]
9   Ryan  NO  [email protected]
========================================

output of i =
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

CodePudding user response:

Try:

 import smtplib
 for name, vip_state, e in df2.values:
    content["to"] = e # receiver
    # name contains the name of the receiver
    content.attach(MIMEText(name,"Demo python send email")) #content
    
    
    with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:  
        try:
            smtp.ehlo()  # 
            smtp.starttls()  # 
            smtp.login("*****.com", "******")  # 
            smtp.send_message(content)  # 
                print("Complete!")
            except Exception as s:
                print("Error message: ", s)

CodePudding user response:

You might need to provide more information on your latest issue or I can just guess what's encountered.

Do you use personal google account to login? Because from May 30, 2022, ​​Google no longer supports the use of third-party apps which uses only your username and password, so you may not able to use it now.

https://support.google.com/accounts/answer/6010255?hl=en&utm_source=google-account&utm_medium=profile-less-secure-apps-card

If you use other smtp server, then maybe give it some time to finish login process:

 import smtplib, time
 for name, vip_state, e in df2.values:
    content["to"] = e # receiver
    # name contains the name of the receiver
    content.attach(MIMEText(name,"Demo python send email")) #content
    
    with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:  
        try:
            time.sleep(1) # <-
            smtp.ehlo()
            smtp.starttls()
            smtp.login("*****.com", "******")
            smtp.send_message(content)
                print("Complete!")
            except Exception as s:
                print("Error message: ", s)
  • Related