ok post the complete code that you have written below in a code block
if it is not giving error is it also not sending any email to the designated email address
import os
import datetime
import sys
import smtplib
from email.message import EmailMessage
#import schedule
#import time
email = EmailMessage()
def remainder_mail():
with open(r"C:\Users\DELL\Documents\schedule.txt") as f:
read_line = f.readlines()
list_lines = [x.strip().split(' ') for x in read_line]
for x in list_lines:
x.remove('-')
date_list = [' '.join(x[0:3])for x in list_lines]
time_list = [' '.join(x[3:5])for x in list_lines]
mod_list = [' '.join(x[5:])for x in list_lines]
comb_time_mod = []
for x in range(len(time_list)):
comb_tm = []
comb_tm.append(time_list[x])
comb_tm.append(mod_list[x])
comb_time_mod.append(comb_tm)
comb_dict = dict(zip(date_list,comb_time_mod))
print(comb_dict)
list_of_lectures = list(comb_dict.keys())
format = "%Y %d %B"
today = datetime.date.today()
format_1 = "%a"
if today.strftime(format_1) in ['Mon','Tue','Wed','Thu']: #checking weekday
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][1]} at {today+datetime.timedelta(days=1)} {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][0]}.Please join class on time"
elif today.strftime(format_1) == 'Fri':
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][1]} at {today+datetime.timedelta(days=3)} {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][0]}.Please join class on time"
#print(a)
mail_content = a
sender_address = sys.argv[1] # sender email id
sender_pass = sys.argv[2] # sender password
receiver_address = ['karan.ahirwar1996@gmail.com','message2arshad@gmail.com','abusufyan732387@gmail.com','nfatma2517@gmail.com']
email['Subject'] ='OneLearn course series reminder'
email['From'] = sender_address
email['To'] =','.join(receiver_address)
email.set_content(mail_content,'plain')
with smtplib.SMTP('smtp.gmail.com',587) as server:
server.starttls()
server.login(sender_address,sender_pass)
server.send_message(email)
print('Mail Sent')
#if __name__=='__main__':
#schedule.every().day.at("10:45").do(remainder_mail)
hi arshad can you tell if the issue is that no email is going to the receiver address? (because in the images there is no error)
no sir i dnt think bcs in simple mail recevier are same and they have recieve mail
ok also paste the code for simple mail receiver.
Just to clarify in simple mail receiver code they were receiving mail but in this code they are not receiving the mail. Is that correct?
import smtplib
from email.message import EmailMessage
email = EmailMessage()
email['Subject'] = 'Testing e-mail'
email['From'] = 'Akhan'
email['To'] =['message@arshad@gmail.com','karan.ahirwar1996@gmail.com', 'nfatma2517@gmail.com','abusufyan732387@gmail.com']
#email['message'] = 'Testing e-mail from arshad khan'
email.set_content('Testing e-mail from arshad khan')
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("infoarshad069@gmail.com" ,"xxclfavdagpuunni")
server.send_message(email)
print('Mail sent')
server.quit()
as far i was understanding the code
server.send_message(email)
here you need to write it in the proper syntax
server.sendmail(<Sender_email>,<receiver_email>,email)
as you are not providing the sender and receiver mailId so it was not able to message
No sir it not works i was changed some code as well in login and sender part as well
import os
import datetime
import sys
import smtplib
from email.message import EmailMessage
#import schedule
#import time
email = EmailMessage()
def remainder_mail():
with open(r"C:\Users\DELL\Documents\schedule.txt") as f:
read_line = f.readlines()
list_lines = [x.strip().split(' ') for x in read_line]
for x in list_lines:
x.remove('-')
date_list = [' '.join(x[0:3])for x in list_lines]
time_list = [' '.join(x[3:5])for x in list_lines]
mod_list = [' '.join(x[5:])for x in list_lines]
comb_time_mod = []
for x in range(len(time_list)):
comb_tm = []
comb_tm.append(time_list[x])
comb_tm.append(mod_list[x])
comb_time_mod.append(comb_tm)
comb_dict = dict(zip(date_list,comb_time_mod))
print(comb_dict)
list_of_lectures = list(comb_dict.keys())
format = "%Y %d %B"
today = datetime.date.today()
format_1 = "%a"
if today.strftime(format_1) in ['Mon','Tue','Wed','Thu']: #checking weekday
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][1]} at {today+datetime.timedelta(days=1)} {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][0]}.Please join class on time"
elif today.strftime(format_1) == 'Fri':
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][1]} at {today+datetime.timedelta(days=3)} {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][0]}.Please join class on time"
#print(a)
mail_content = a
#sender_address = sys.argv[1] # sender email id
#sender_pass = sys.argv[2] # sender password
receiver_address = ['karan.ahirwar1996@gmail.com','message2arshad@gmail.com','abusufyan732387@gmail.com','nfatma2517@gmail.com']
email['Subject'] ='OneLearn course series reminder'
email['From'] = 'Akhan'
email['To'] =','.join(receiver_address)
email.set_content(mail_content,'plain')
with smtplib.SMTP('smtp.gmail.com',587) as server:
server.starttls()
server.login("infoarshad069@gmail.com","xxclfavdagpuunni")
server.send_message('infoarshad069@gmail.com','karan.ahirwar1996@gmail.com','nfatma@2517@gmail.com','message2arshad@gmail.com',email)
print('Mail Sent')
#if __name__=='__main__':
#schedule.every().day.at("10:45").do(remainder_mail)
a few clear mistakes that i can observe directly from code is using x
variable name multiple times in loops. This is not a good practice usually when you do nested looping you should use different loop variable names.
Another clear mistake is the first loop where you are looping over list_line and then inside that loop again looping over list_lines to create date_list and other things. This means you are looping over the same thing twice inside 1 loop, you could have done things with just 1 loop. This creates wrong type of variables as you can see from the image below. (check the red square box there time should be coming up but something entirely else is coming.) Also observe how i debugged by just printing what each variable looks like below. This is what you should also do to debug any code.
ok arshad let’s start the debugging process. Please run the below code. I have added a few print statements in between the code to check if the code is working till that point or not (printing in between to check the issue with code is a standard practice - do adopt while solving these kinds of issues). after you run the code please check if the print statements make sense or not. Also paste the output of the code in the next reply.
Another clear issue with whole code is that you are sending mails for all dates in schedule.txt but you july and august are already over, sending mails for them does not make sense.
I also think you should check how praneeth wrote the code for this in his tutorial. This is not how he wrote it. I thought you have seen the video explanation.
You can check it here.
sir in this code also nothing will print and in my schedule.txt file i update also in sep month and i saw the tutorial of praneet sir and i do almost same
Did you comment out the last if part.
If that code is not working , did you just put in this line at the end of the code
remainder_mail()
because otherwise remainder_mail function will never get called and nothing will work inside it
No sir now my codeis only this
import os
import datetime
import sys
import smtplib
from email.message import EmailMessage
#import schedule
#import time
email = EmailMessage()
def remainder_mail():
with open(r"C:\Users\DELL\Documents\schedule.txt") as f:
read_line = f.readlines()
list_lines = [x.strip().split(' ') for x in read_line]
for x in list_lines:
x.remove('-')
date_list = [' '.join(x[0:3])for x in list_lines]
time_list = [' '.join(x[3:5])for x in list_lines]
mod_list = [' '.join(x[5:])for x in list_lines]
print(date_list)
print(time_list)
print(mod_list)
comb_time_mod = []
for x in range(len(time_list)):
comb_tm = []
comb_tm.append(time_list[x])
comb_tm.append(mod_list[x])
comb_time_mod.append(comb_tm)
print(comb_tm)
comb_dict = dict(zip(date_list,comb_time_mod))
print(comb_dict)
list_of_lectures = list(comb_dict.keys())
format = "%Y %d %B"
today = datetime.date.today()
format_1 = "%a"
if today.strftime(format_1) in ['Mon','Tue','Wed','Thu']: #checking weekday
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][1]} at {today+datetime.timedelta(days=1)} {comb_dict[(today+datetime.timedelta(days=1)).strftime(format)][0]}.Please join class on time"
elif today.strftime(format_1) == 'Fri':
a = f"This is a reminder mail for your class on {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][1]} at {today+datetime.timedelta(days=3)} {comb_dict[(today+datetime.timedelta(days=3)).strftime(format)][0]}.Please join class on time"
#print(a)
mail_content = a
#sender_address = sys.argv[1] # sender email id
#sender_pass = sys.argv[2] # sender password
receiver_address = ['karan.ahirwar1996@gmail.com','message2arshad@gmail.com','abusufyan732387@gmail.com','nfatma2517@gmail.com']
email['Subject'] ='OneLearn course series reminder'
email['From'] = 'Akhan'
email['To'] =','.join(receiver_address)
email.set_content(mail_content,'plain')
with smtplib.SMTP('smtp.gmail.com',587) as server:
print('going inside the with part')
server.starttls()
server.login("infoarshad069@gmail.com","xxclfavdagpuunni")
server.send_message('infoarshad069@gmail.com','karan.ahirwar1996@gmail.com','nfatma@2517@gmail.com','message2arshad@gmail.com',email)
print('Mail Sent')
ok arshad i have added remainder_mail() at the end of the script.Now that function will be called and your script might work. This is a very basic mistake but no issues, the main lesson here is whenever you create a function after that you need to again call it somewhere by function name like this func_name()
Also if schedule import is not working then do
pip install schedule
in terminal. This will install schedule inside python and then that import will work.
sir this error will come
resent = msg.get_all('Resent-Date')
AttributeError: 'str' object has no attribute 'get_all'
output is
PS C:\Users\DELL\onelearn\Temp_try_new> & C:/Users/DELL/AppData/Local/Programs/Python/Python310/python.exe c:/Users/DELL/onelearn/Temp_try_new/autoemail/autoemail.py
['2022 19 September', '2022 20 September', '2022 21 September', '2022 22 September', '2022 23 September']
['12:00 PM', '12:00 PM', '12:00 PM', '12:00 PM', '12:00 PM']
['Class 1 Mod 2', '- Class 2 Mod 2', '- Class 3 Mod 2', '- Class 4 Mod 2', '- Class 5 Mod 2']
['12:00 PM', 'Class 1 Mod 2']
{'2022 19 September': ['12:00 PM', 'Class 1 Mod 2']}
going inside the with part
Traceback (most recent call last):
File "c:\Users\DELL\onelearn\Temp_try_new\autoemail\autoemail.py", line 66, in <module>
remainder_mail()
File "c:\Users\DELL\onelearn\Temp_try_new\autoemail\autoemail.py", line 64, in remainder_mail
server.send_message('infoarshad069@gmail.com','karan.ahirwar1996@gmail.com','nfatma@2517@gmail.com','message2arshad@gmail.com',email)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 944, in send_message
resent = msg.get_all('Resent-Date')
AttributeError: 'str' object has no attribute 'get_all'
PS C:\Users\DELL\onelearn\Temp_try_new>