Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
mentors.debian.net
debexpo
Commits
0df14c44
Verified
Commit
0df14c44
authored
Apr 02, 2020
by
Baptiste Beauplat
Browse files
Catch socket/SMTP exception when sending emails
parent
0130c063
Changes
1
Hide whitespace changes
Inline
Side-by-side
debexpo/lib/email.py
View file @
0df14c44
...
@@ -170,15 +170,21 @@ class Email(object):
...
@@ -170,15 +170,21 @@ class Email(object):
pylons
.
url
.
_pop_object
()
pylons
.
url
.
_pop_object
()
if
'debexpo.testsmtp'
in
pylons
.
config
:
if
'debexpo.testsmtp'
in
pylons
.
config
:
self
.
_save_as_file
(
recipients
,
message
)
return
self
.
_save_as_file
(
recipients
,
message
)
else
:
else
:
self
.
_send_as_mail
(
recipients
,
message
)
try
:
return
self
.
_send_as_mail
(
recipients
,
message
)
except
(
IOError
,
smtplib
.
SMTPException
)
as
e
:
log
.
critical
(
'Failed to send email: {}'
.
format
(
e
))
return
False
def
_save_as_file
(
self
,
recipients
,
message
):
def
_save_as_file
(
self
,
recipients
,
message
):
log
.
debug
(
'Save email as file to %s'
%
self
.
server
)
log
.
debug
(
'Save email as file to %s'
%
self
.
server
)
with
open
(
pylons
.
config
[
'debexpo.testsmtp'
],
'a'
)
as
email
:
with
open
(
pylons
.
config
[
'debexpo.testsmtp'
],
'a'
)
as
email
:
email
.
write
(
message
)
email
.
write
(
message
)
return
True
def
_send_as_mail
(
self
,
recipients
,
message
):
def
_send_as_mail
(
self
,
recipients
,
message
):
log
.
debug
(
'Starting SMTP session to %s:%s'
%
(
self
.
server
,
self
.
port
))
log
.
debug
(
'Starting SMTP session to %s:%s'
%
(
self
.
server
,
self
.
port
))
session
=
smtplib
.
SMTP
(
self
.
server
,
self
.
port
)
session
=
smtplib
.
SMTP
(
self
.
server
,
self
.
port
)
...
@@ -197,8 +203,10 @@ class Email(object):
...
@@ -197,8 +203,10 @@ class Email(object):
log
.
critical
(
'Failed sending to %s: %s, %s'
%
log
.
critical
(
'Failed sending to %s: %s, %s'
%
(
recipient
,
result
[
recipient
][
0
],
(
recipient
,
result
[
recipient
][
0
],
result
[
recipient
][
1
]))
result
[
recipient
][
1
]))
else
:
return
False
log
.
debug
(
'Successfully sent'
)
log
.
debug
(
'Successfully sent'
)
return
True
def
_check_error
(
self
,
msg
,
err
,
data
=
None
):
def
_check_error
(
self
,
msg
,
err
,
data
=
None
):
if
err
!=
'OK'
:
if
err
!=
'OK'
:
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment