Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77
main.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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]) |