Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
news-service-sender-python/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
77 lines (57 sloc)
1.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv, smtplib, ssl | |
import requests | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
import mysql.connector as mysql | |
def NewsFromBBC(): | |
# BBC news api | |
main_url = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=4dbc17e007ab436fb66416009dfb59a8" | |
open_bbc_page = requests.get(main_url).json() | |
article = open_bbc_page["articles"] | |
results = [] | |
news ='' | |
for ar in article: | |
results.append(ar["title"]) | |
#return results | |
for i in range(len(results)): | |
print(i + 1, results[i]) | |
news = news +str(i + 1)+' ' +results[i] +'<br/>' | |
return news | |
def sendmail(recevier_address): | |
text = """\ | |
Hi, | |
Todays headlines are :- | |
""" | |
html = NewsFromBBC() | |
from_address = "temailservice@gmail.com" | |
password = "qwerty@123" #input("Type your password and press enter: ") | |
message = MIMEMultipart("alternative") | |
message["Subj"] = "Today's News" | |
message["From"] = from_address | |
message["To"] = recevier_address | |
part1 = MIMEText(text, "plain") | |
part2 = MIMEText(html, "html") | |
message.attach(part1) | |
message.attach(part2) | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: | |
server.login(from_address, password) | |
server.sendmail(from_address, recevier_address, message.as_string()) | |
db = mysql.connect( | |
host = "localhost", | |
port="3307", | |
user = "root", | |
passwd = "", | |
database = "subscribers" | |
) | |
cursor = db.cursor() | |
## defining the Query | |
query = "SELECT Email FROM person" | |
## getting records from the table | |
cursor.execute(query) | |
## fetching all records from the 'cursor' object | |
records = cursor.fetchall() | |
## Showing the data | |
for record in records: | |
print(record[0]) | |
sendmail(record[0]) |