· 2 years ago · Apr 16, 2023, 01:40 PM
1import requests
2import json
3import html
4import os
5import getpass
6from cryptography.fernet import Fernet
7
8# Define the name of the file that stores the encryption key
9KEY_FILENAME = 'encryption_key.txt'
10
11def encrypt_token(token, key):
12 f = Fernet(key)
13 encrypted_token = f.encrypt(token.encode())
14 with open('encrypted_token.txt', 'wb') as file:
15 file.write(encrypted_token)
16
17def decrypt_token(key):
18 with open('encrypted_token.txt', 'rb') as file:
19 encrypted_token = file.read()
20 f = Fernet(key)
21 auth_token = f.decrypt(encrypted_token).decode()
22 return auth_token
23
24# Check if the encryption key file exists, and create it if it doesn't
25if not os.path.exists(KEY_FILENAME):
26 key = Fernet.generate_key()
27 with open(KEY_FILENAME, 'wb') as key_file:
28 key_file.write(key)
29else:
30 with open(KEY_FILENAME, 'rb') as key_file:
31 key = key_file.read()
32
33# Check if the encrypted token file exists, and prompt the user to enter the auth token if it doesn't
34if not os.path.exists('encrypted_token.txt'):
35 auth_token = getpass.getpass('Enter authentication token: ')
36 encrypt_token(auth_token, key)
37else:
38 auth_token = decrypt_token(key)
39
40url = 'https://zeevest.000webhostapp.com/authenticate.php'
41payload = {'authentication_token': auth_token}
42response = requests.post(url, data=payload)
43
44# Check if the authentication succeeded
45if response.text == 'Authentication succeeded.':
46 # Perform your desired action here
47 print('Authentication succeeded.')
48 url2 = 'https://zeevest.000webhostapp.com/get-api.php'
49 response = requests.get(url2)
50
51 if response.status_code == 200:
52 # Store the response in a variable named api
53 api_key = response.text
54 else:
55 # Print an error message if the request failed
56 print('Error: Unable to get Api Keys')
57 api_key = input("Enter your Sendinblue API key: ")
58
59
60 # Prompt the user to enter the email content
61 sender_name = input("Enter the sender name: ")
62 sender_email = input("Enter the sender email: ")
63 subject = input("Enter the email subject: ")
64 message = input("""Enter the email message: """)
65
66 # Format the message as HTML
67 html_message = f"<html><body>{html.escape(message)}</body></html>"
68
69 # Read the list of email addresses from the file
70 with open('email_list.txt') as f:
71 recipient_emails = [line.strip() for line in f]
72
73 # Loop over the recipient email addresses and send the email to each address
74 for email in recipient_emails:
75 # Check if the email address already exists in the sent_emails.txt file
76 with open('sent_emails.txt', 'r') as sent_emails_file:
77 if email in sent_emails_file.read():
78 print(f'{email} has already been sent an email.')
79 continue
80
81 # Define the email data
82 email_data = {
83 "sender": {"name": sender_name, "email": sender_email},
84 "to": [{"email": email}],
85 "subject": subject,
86 "htmlContent": html_message
87 }
88
89 # Convert the email data to JSON format
90 email_json = json.dumps(email_data)
91
92 # Set the request headers
93 headers = {
94 'Content-Type': 'application/json',
95 'api-key': api_key
96 }
97
98 # Define the Sendinblue API endpoint URL
99 SENDINBLUE_API_ENDPOINT = 'https://api.sendinblue.com/v3/smtp/email'
100
101 # Send the email
102 response = requests.post(SENDINBLUE_API_ENDPOINT, headers=headers, data=email_json)
103
104 # Check the response status
105 if response.status_code == 201:
106 print(f'Email sent successfully to {email}!')
107 # Append the email address to the sent_emails.txt file
108 with open('sent_emails.txt', 'a') as sent_emails_file:
109 sent_emails_file.write(email + '\n')
110 else:
111 print(f'Failed to send email to {email}:', response.json())
112
113else:
114 # Authentication failed, print an error message
115 print('Authentication failed.')
116 if os.path.exists('encrypted_token.txt'):
117 os.remove('encrypted_token.txt')
118 if os.path.exists('encryption_key.txt'):
119 os.remove('encryption_key.txt')
120 # Prompt the user to enter a new authentication token
121 auth_token = getpass.getpass('Enter new authentication token: ')
122 key = encrypt_token(auth_token)
123 url = 'https://zeevest.000webhostapp.com/authenticate.php'
124 payload = {'authentication_token': auth_token}
125 response = requests.post(url, data=payload)
126 if response.text == 'Authentication succeeded.':
127 print('Authentication succeeded.')
128 with open('encryption_key.txt', 'wb') as file:
129 file.write(key)
130 else:
131 print('Authentication failed again. Please check your authentication token and try again later.')
132