-1

Basically I'm creating a program to help with my work. It will send emails to people in an excel list and move down to the next first name and email address in the list until it's done. Heres the code so far

`#AutoMail Version 2
#Goal of new version is to run on any computer. With minimal or no mouse and keyboard input
import pandas as pd 
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#Random Variables
sender_address = str(input("Please enter your email address!: "))
sender_pass = str(input("Please enter your email password (No data is stored anywhere!): "))
count = 0

#This prompts user to input the file path of their CSV file.
file_path = "C:/Users/Spring/Documents/test_book_py.csv" #Change to input later!!!!!!
df = pd.read_csv(file_path, usecols=['First Name', 'Email Address'])
amount = int(input("How many emails would you like to send? "))

#Important Variables
cell_value = 0 #Which cell the info is coming from

#Cell Varialbes
name_cell = df["First Name"].values[cell_value]
email_cell = df["Email Address"].values[cell_value]

#Gmail info Variables
receiver_address = email_cell
email_subj = "This is a test subject"
email_body = "Hello " + name_cell + ",\n\nThis is a test body"
message = MIMEMultipart()

#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password

#Emailing Process Start
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = email_subj
message.attach(MIMEText(email_body, 'plain'))
text = message.as_string()

#Email sending
while count < amount:
    session.sendmail(sender_address, receiver_address, text)
    cell_value = cell_value + 1
    count = count + 1

print(cell_value)`

I've tried every fix I could find online for variables not updating. When I print the "cell_value" varible it prints with the updated value however the other lines in the code specifically lines 21 and 22 use that variable and they aren't using the updated varible so it is always at a constant 0 value when it should be cell_value + 1 every time the loop repeats. Is there a different way I should loop the variable updating? I need it to change that value by +1 every time so that it continues to move down the list. Keep in mind that I am a huge beginner so my code probably looks very confusing.

gcb140
  • 19
  • 2
  • 2
    You run `name_cell = df["First Name"].values[cell_value]` before ever changing `cell_value`. All the data that used the original `name_cell` value doesn't automatically update when `cell_value` is changed. You'd need to loop back up to line ~21 and rerun all that code to recalculate the values with the new `cell_value` value. – Carcigenicate Nov 30 '22 at 01:15
  • Sorry if this is a stupid question but how would you recommend that I do that? – gcb140 Nov 30 '22 at 01:39

1 Answers1

0

The issue is updating cell_value doesn't automatically updates all the data that was calculated with cell_value's old value. Once "Hello " + name_cell + ",\n\nThis is a test body" evaluates, for example, the resulting string has no relation to name_cell, and wan't change when name_cell changes. If you want that string to change when name_cell changes, you need to rerun the code that created that string.

For your case here, it looks like you could just loop over the latter half of the code. The closest to what you already have would be:

# i instead of cell_value for clarity
for i in range(amount):
    name_cell = df["First Name"].values[cell_value]
    email_cell = df["Email Address"].values[cell_value]
    
    receiver_address = email_cell
    email_subj = "This is a test subject"
    email_body = "Hello " + name_cell + ",\n\nThis is a test body"
    message = MIMEMultipart()
    
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(sender_address, sender_pass) #login with mail_id and password
    
    message['From'] = sender_address
    message['To'] = receiver_address
    message['Subject'] = email_subj
    message.attach(MIMEText(email_body, 'plain'))
    text = message.as_string()
    
    session.sendmail(sender_address, receiver_address, text)

Arguably, it would be may be considered more idiomatic to zip the two .values objects that you're looping over, then islice amount-many elements from that, but I think this is cleaner.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117