-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
58 lines (49 loc) · 2.24 KB
/
mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from azure.communication.email import EmailClient
class Mail:
def __init__(self,mail_password,mail_sender_email):
self.sender_email = mail_sender_email
self.smtp_server = 'smtp.gmail.com'
self.smtp_port = 587
self.username = self.sender_email
self.password = mail_password
self.connection_string = "endpoint=https://blessi-communication.india.communication.azure.com/;accesskey=Gw7gYwrvSE6yGC+SteewLi7UjFEZl8wSqkEB2NuPtGh98iLSmgqeFyjk7036QJnTXEQPKWLOBRCJVeFYbb1UFg=="
self.from_address = "[email protected]"
#you can make a custom email to mail through this ai model or you can use gmail smtp service just config it according to your needs
def send_mail(self,receiver_mail,subject,body):
receiver_mail=receiver_mail.replace(" ", "")
message = MIMEMultipart()
print("MIME clear")
message['From'] = self.sender_email
message['To'] = receiver_mail
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
print("MIME text")
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
print("ttl started")
server.login(self.username, "lsvswnjrgktyncld")
print("user logined")
server.sendmail(self.sender_email, receiver_mail, message.as_string())
print("send succ")
print("Email sent successfully.")
def send_blessi_email(self, to_address, subject, body):
try:
client = EmailClient.from_connection_string(self.connection_string)
email_message = {
"senderAddress": self.from_address,
"recipients": {
"to": [{"address": to_address}],
},
"content": {
"subject": subject,
"plainText":body,
}
}
send_email_poller = client.begin_send(email_message)
result = send_email_poller.result()
print("Email sent successfully:", result)
except Exception as ex:
print(f"An error occurred: {ex}")