This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mail service and use case (invite firend) done NOT TESTED (#58)
* Mail service and use case (invite firend)
- Loading branch information
Showing
10 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
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,18 @@ | ||
import inject | ||
from thesheriff.domain.mail.mail import Mail | ||
from thesheriff.domain.mail.repository.mail_notification \ | ||
import MailNotification | ||
from thesheriff.domain.outlaw.outlaw import Outlaw | ||
|
||
|
||
class InviteFriend: | ||
|
||
@inject.autoparams() | ||
def __init__(self, mail_repository: MailNotification): | ||
self.mail_repository = mail_repository | ||
|
||
def execute(self, receiver_mail_address): | ||
sender = Outlaw.get_email() | ||
receiver = receiver_mail_address | ||
mail = Mail.mail_invite_friend(sender, receiver) | ||
self.mail_repository.send(mail) |
Empty file.
Empty file.
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,23 @@ | ||
import string | ||
|
||
|
||
class Mail: | ||
def __init__(self): | ||
sender = "" | ||
receiver = "" | ||
content = "" | ||
|
||
def mail_invite_friend(self, sender, receiver): | ||
self.sender = sender | ||
self.receiver = receiver | ||
self.content = "Hello dear " + str(receiver) + "!\n " \ | ||
"Do you want to join this awesome game?" + \ | ||
"Come on!" | ||
|
||
if sender is None: | ||
raise Exception('User address needed to send an invitation') | ||
if receiver is None: | ||
raise Exception('Destination address needed to send an invitation') | ||
|
||
def send_notification_raid(self): | ||
return |
Empty file.
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,9 @@ | ||
import abc | ||
from thesheriff.domain.mail.mail import Mail | ||
|
||
|
||
class MailNotification(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def send(self, mail: Mail): | ||
pass |
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
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
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
26 changes: 26 additions & 0 deletions
26
thesheriff/infrastructure/repository/smtp_mail_repository.py
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,26 @@ | ||
import smtplib | ||
import ssl | ||
from thesheriff.domain.mail.repository.mail_notification \ | ||
import MailNotification | ||
|
||
|
||
class SMTPMailRepository: | ||
global port # For SSL | ||
global context | ||
global password | ||
|
||
# Create a secure SSL context | ||
port = 465 | ||
context = ssl.create_default_context() | ||
# TODO(all): read the password from an environment variable | ||
password = "thesheriff123" | ||
|
||
def __init__(self, mail_repository: MailNotification): | ||
self.mail_repository = mail_repository | ||
|
||
def send(self, mail): | ||
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) \ | ||
as server: | ||
# To send the email, we log into the system with the sender address | ||
server.login(mail.sender, password) | ||
server.sendmail(mail.sender, mail.receiver, mail.content) |