Send email to multiple recipients which contains URL from file

I have had the need to send mutiple email messages which contains for each one specific URL, this message had to be sent to multiple recipients.

So do so, I have decided to look at using python; As most likely my current OS machine was Linux, as on linux python 2.x is native to it.

First of all, you do need the list of URL (url.txt) and also the list of recipients (rcpt.txt).

the URL file:

www.url1.com
www.url2.com

then, the recipient file:

user1@domain.com
user2@domain.com
etc

 

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = "sender@domain.com"

with open('url.txt') as fp:
	for line in fp:
		print(line)
		msg = MIMEMultipart('alternative')
		
		msg['From'] = str(sender)
		
		msg['Subject'] = line
		body = "This is the body of the message.\nNothing more.\n"

		text = "Hi!\nHow are you?\nThis is the link that you need.\n"
		html = """
		Hello,
		How are you?
		This is the html body of the message
		

""" +str(line)+ """

Here is the URL you require: """ +str(line)+ """   """ part1 = MIMEText(text, 'line') part2 = MIMEText(html, 'html') msg.attach(part1) msg.attach(MIMEText(html, 'html')) with open('rcpt.txt') as send: for rcpt in send: msg['To'] = rcpt # if not authentication; uncomment line server and comment the three which follows. #server = smtplib.SMTP('SMTP_IP', 25) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("gmail user account", "password") text = msg.as_string() server.sendmail(sender, rcpt, text) print "Message sent for " +str(rcpt) server.quit()

Leave a Reply

Your email address will not be published. Required fields are marked *