Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scheduling-system
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SMASCH
scheduling-system
Commits
39939d7f
There was a problem fetching the pipeline metadata.
Commit
39939d7f
authored
7 years ago
by
Piotr Gawron
Browse files
Options
Downloads
Patches
Plain Diff
send email functionality
parent
f8345c34
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!42
Email functionality
Pipeline
#
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
smash/web/email.py
+20
-22
20 additions, 22 deletions
smash/web/email.py
smash/web/tests/test_email.py
+8
-20
8 additions, 20 deletions
smash/web/tests/test_email.py
with
28 additions
and
42 deletions
smash/web/email.py
+
20
−
22
View file @
39939d7f
# coding=utf-8
from
django.core
import
mail
from
django.core.mail.backends.smtp
import
EmailBackend
from
web.models
import
ConfigurationItem
from
web.models.constants
import
EMAIL_HOST_CONFIGURATION_TYPE
,
EMAIL_PASSWORD_CONFIGURATION_TYPE
,
\
EMAIL_PORT_CONFIGURATION_TYPE
,
EMAIL_USER_CONFIGURATION_TYPE
,
EMAIL_CONFIGURATION_TYPE
from
django.conf
import
settings
from
django.core.mail
import
send_mail
from
django.core.mail
import
EmailMessage
class
EmailSender
(
object
):
def
__init__
(
self
):
host
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_HOST_CONFIGURATION_TYPE
).
value
port
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_PORT_CONFIGURATION_TYPE
).
value
user
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_USER_CONFIGURATION_TYPE
).
value
password
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_PASSWORD_CONFIGURATION_TYPE
).
value
self
.
backend
=
EmailBackend
(
host
=
host
,
port
=
port
,
username
=
user
,
password
=
password
,
use_tls
=
False
,
use_ssl
=
False
)
# self.connection = mail.get_connection(backend=self.backend)
self
.
connection
=
mail
.
get_connection
()
def
send_email
(
self
,
subject
,
body
,
recipients
,
cc_recipients
=
[]):
email_from
=
getattr
(
settings
,
"
DEFAULT_FROM_EMAIL
"
,
None
)
recipient_list
=
[]
for
recipient
in
recipients
.
split
(
"
;
"
):
recipient_list
.
append
(
recipient
)
cc_recipients
.
append
(
email_from
)
message
=
EmailMessage
(
subject
,
body
,
email_from
,
recipient_list
,
cc
=
cc_recipients
)
message
.
content_subtype
=
"
html
"
message
.
send
()
def
send_email
(
self
,
subject
,
body
,
to
):
email_from
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_CONFIGURATION_TYPE
).
value
print
email_from
print
subject
print
body
print
to
mail
.
EmailMessage
(
subject
,
body
,
email_from
,
[
to
],
connection
=
self
.
connection
).
send
(
fail_silently
=
False
)
# send_mail(subject, "", email_from, recipient_list, cc=cc_recipients, html_message=body)
This diff is collapsed.
Click to expand it.
smash/web/tests/test_email.py
+
8
−
20
View file @
39939d7f
# coding=utf-8
from
django.core
import
mail
from
django.test
import
TestCase
from
web.email
import
EmailSender
from
web.models
import
ConfigurationItem
from
web.models.constants
import
EMAIL_HOST_CONFIGURATION_TYPE
,
EMAIL_PASSWORD_CONFIGURATION_TYPE
,
\
EMAIL_PORT_CONFIGURATION_TYPE
,
EMAIL_USER_CONFIGURATION_TYPE
,
EMAIL_CONFIGURATION_TYPE
class
TestEmailSender
(
TestCase
):
def
setUp
(
selfself
):
host
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_HOST_CONFIGURATION_TYPE
)
host
.
value
=
"
smtp.uni.lu
"
host
.
save
()
port
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_PORT_CONFIGURATION_TYPE
)
port
.
value
=
"
25
"
port
.
save
()
# uni doesn't check it
user
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_USER_CONFIGURATION_TYPE
)
password
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_PASSWORD_CONFIGURATION_TYPE
)
email_from
=
ConfigurationItem
.
objects
.
get
(
type
=
EMAIL_CONFIGURATION_TYPE
)
email_from
.
value
=
"
piotr.gawron@uni.lu
"
email_from
.
save
()
def
test_send_email
(
self
):
self
.
assertEqual
(
0
,
len
(
mail
.
outbox
))
email_sender
=
EmailSender
()
email_sender
.
send_email
(
"
test
"
,
"
test body<br/>and content
"
,
"
piotr.gawron@uni.lu
"
)
self
.
assertEqual
(
1
,
len
(
mail
.
outbox
))
# console test that actually send email
# > manage.py shell
# from web.email import EmailSender; EmailSender().send_email("test","<h1>body</h1>","piotr.gawron@uni.lu");
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment