send email to users when free trial expires

This commit is contained in:
MuslemRahimi 2024-08-15 17:23:11 +02:00
parent 6fb6816167
commit 6642d11f08
3 changed files with 98 additions and 3 deletions

View File

@ -1,21 +1,95 @@
import pytz
from datetime import datetime, timedelta
from urllib.request import urlopen
import certifi
import json
import ujson
import schedule
import time
import subprocess
from pocketbase import PocketBase # Client also works the same
import asyncio
import aiohttp
import pytz
import pandas as pd
import numpy as np
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import boto3
from botocore.exceptions import NoCredentialsError
from bs4 import BeautifulSoup
from dotenv import load_dotenv
import os
load_dotenv()
load_dotenv()
api_key = os.getenv('FMP_API_KEY')
pb_admin_email = os.getenv('POCKETBASE_ADMIN_EMAIL')
pb_password = os.getenv('POCKETBASE_PASSWORD')
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
berlin_tz = pytz.timezone('Europe/Berlin')
pb = PocketBase('http://127.0.0.1:8090')
admin_data = pb.admins.auth_with_password(pb_admin_email, pb_password)
pb_admin_email = os.getenv('POCKETBASE_ADMIN_EMAIL')
pb_password = os.getenv('POCKETBASE_PASSWORD')
now = datetime.now()
one_month_ago = now - timedelta(days=30)
def send_email(recipient):
# Replace the placeholders with your AWS SES credentials
region_name = 'eu-north-1' #email-smtp.eu-north-1.amazonaws.com
# Replace the placeholders with your sender email and password
sender_email = 'mrahimi@stocknear.com'
to_email = recipient # user email address
subject = f'Your Free Trial expired'
# Read the index.html file
with open('html_template/free_trial.html', 'r') as file:
html_content = file.read()
# Create a MIMEMultipart object
message = MIMEMultipart('alternative')
message['Subject'] = subject
message['From'] = sender_email
message['To'] = to_email
#Preheader text
preheader = MIMEText("Your free trial ended.", 'plain')
message.attach(MIMEText(html_content, 'html'))
# Use Amazon SES to send the email
ses_client = boto3.client(
'ses',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)
try:
# Send the email
response = ses_client.send_raw_email(
Source=message['From'],
Destinations=[message['To']],
RawMessage={'Data': message.as_string()},
)
print("Email sent successfully!")
except NoCredentialsError:
print("AWS credentials not available")
except Exception as e:
print(f"Error sending email: {e}")
async def update_free_trial():
data = pb.collection("users").get_full_list(query_params = {"filter": f'freeTrial = True'})
@ -29,5 +103,10 @@ async def update_free_trial():
"tier": 'Free',
"freeTrial": False,
})
try:
send_email(item.email)
except Exception as e:
print(e)
asyncio.run(update_free_trial())

View File

@ -147,7 +147,7 @@ async def run():
}
pb.collection('notifications').create(newNotification)
#send alert via email
recipient = (pb.collection('users').get_one(item.user)).email
recipient = item.email
send_email(recipient, symbol, item.asset_type, current_price, target_price, item.condition)
try:
asyncio.run(run())

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en" class="bg-[#fff]">
<div> <body><div><div><center style="width:100%;table-layout:fixed;background-color:#ffffff;padding-bottom:60px"><table role="presentation" width="100%" style="background-color:#ffffff;margin:0 auto;width:100%;max-width:600px;border-spacing:0;font-family:Arial,Helvetica,sans-serif;color:#171a1b"><tbody><tr data-svelte-h="svelte-1vdj9o4"><td height="6" style="padding:0"></td></tr> <tr><td style="padding:0;border:1px solid #d1d5db;border-radius:5px;padding:15px 10px 15px 10px"><table role="presentation" width="100%" style="border-spacing: 0;"><tbody><tr data-svelte-h="svelte-1mqdbj9"><td height="8"></td></tr> <tr><td align="center"><a href="https://stocknear.com" target="_blank" rel="noopener" style="color: inherit; text-decoration: none;"><img src="https://d1cwu67pi1m619.cloudfront.net/assets/email_logo.jpg" width="60" alt="Logo" style="border: 0; margin-right: 10px; border-radius: 30px;"> <h1 style="font-weight: 700; font-size: 1.3rem;" data-svelte-h="svelte-mbj7s0">Stocknear</h1></a></td></tr></tbody></table></td></tr> <tr data-svelte-h="svelte-1nnezjk"><td style="padding: 15px; border: 1px solid #d1d5db; border-radius: 5px;"><table role="presentation" width="100%" style="border-spacing: 0;"><tbody><tr><td style="padding: 0;"><h2 style="margin-bottom: 20px; padding: 0; font-weight: 700; font-size: 1.2rem;">Free Trial</h2></td></tr> <tr><td height="2" style="padding: 0;"></td></tr> <tr><td style="padding: 0;"><table role="presentation" width="100%" style="border-spacing: 0;"><tbody><tr><td style="padding: 0;"><ul style="padding-left: 25px;"><li style="color: #323232; font-size: 16px; line-height: 22px; margin-bottom: 20px;">Your free trial has ended. To continue enjoying unlimited access to the platform and support our project, please consider subscribing to the
<a href="https://stocknear.com/pricing" target="_blank" rel="noopener nofollow" style="color:#1e73ba;">Pro Tier.</a></li> <li style="color: #323232; font-size: 16px; line-height: 22px; margin-bottom: 20px;">To cover the costs of service, servers, and data licensing, we offer a monthly subscription at $1.99 or an annual plan at $19.99, saving you an additional 16%.</li> <li style="color: #323232; font-size: 16px; line-height: 22px; margin-bottom: 20px;">Stocknears primary goal is to remain transparent and consumer-friendly. Consider this an experiment to see if a project built on ethical design can thrive, compared to others in the industry that are often abusive and absurdly priced.</li></ul></td></tr></tbody></table></td></tr></tbody></table></td></tr> <tr data-svelte-h="svelte-1ngj2oo"><td height="20" style="padding:0"></td></tr> <table role="presentation" width="100%" style="border-spacing:0" data-svelte-h="svelte-31ebie"><tbody><tr><td style="padding-top:10px;width:3%"></td> <td align="center" style="padding:0"><a href="https://stocknear.com" target="_blank" rel="noopener" style="color:#1e73ba;;font-size:16px;margin:0 3px;line-height:2em" data-saferedirecturl="https://stocknear.com">Home Page</a> |
<a href="https://stocknear.com/pricing" target="_blank" rel="noopener" style="color:#1e73ba;;font-size:16px;margin:0 3px;line-height:2em">Pricing</a> |
<a href="https://stocknear.com/about" target="_blank" rel="noopener" style="color:#1e73ba;;font-size:16px;margin:0 3px;line-height:2em" data-saferedirecturl="https://stocknear.com/about">About Us</a></td> <td style="padding:0;width:3%"></td></tr></tbody></table></tbody></table></center></div></div></body>
</div>
</body>
</html>