Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import imaplib
import email
from email.parser import Parser
from twilio.rest import Client
import time

def msg(data):
	result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID 
	raw_email = data[0][1]
	headers = Parser().parsestr(raw_email)
	# prints content in console
	print 'To: %s' % headers['to']
	print 'From: %s' % headers['from']
	print 'Subject: %s' % headers['subject']
	# Your Account Sid and Auth Token from twilio.com/console
	# create a phone number from TWilio site then you will get account_sid and auth_token for that number
	account_sid = 'AC839821af00843ef0386180fcdd51ef84'
	auth_token = '005e30fc72e7f9253db1193741a4a066'
	client = Client(account_sid, auth_token)
	# composing SMS
	message = client.messages \
	    .create(
	         body='From: %s' % headers['from']+'Subject: %s' % headers['subject'],
	         from_='+13862226833',# this number i got from twilio
	         to='+917997623352'# mobile number to which you want to get SMs
	     )
	print(message.sid)
	



mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('#mail id','#mail password')
mail.list()
mail.select('inbox')
result, data = mail.search(None,'ALL')
#retrieves the latest (newest) email by sequential ID
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
msg(data)

while True:
	time.sleep(01)
	mail = imaplib.IMAP4_SSL('imap.gmail.com')
	mail.login('#your mail-id','#mail password')
	mail.list()
	mail.select('inbox')
	#retrieves the latest (newest) email by sequential ID
	result, data1 = mail.search(None,'ALL')
	ids1 = data1[0]
	id_list = ids1.split()
	latest_email_id = id_list[-1]
	if(ids!=ids1):
		msg(data1)
		ids=ids1


What I have tried:

I am unable to understand this code... please help me
Posted
Updated 28-Mar-21 20:29pm
Comments
Richard MacCutchan 29-Mar-21 3:43am    
It is a simple mail program.

1 solution

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900