]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - backend/messages.py
8 from email
.mime
.text
import MIMEText
12 class Messages(base
.Object
):
13 def add(self
, to
, subject
, text
, frm
=None):
14 subject
= "%s %s" % (self
.pakfire
.settings
.get("email_subject_prefix"), subject
)
16 # Get default sender from the settings.
18 frm
= self
.pakfire
.settings
.get("email_from")
20 self
.db
.execute("INSERT INTO user_messages(frm, `to`, subject, text)"
21 " VALUES(%s, %s, %s, %s)", frm
, to
, subject
, text
)
23 def get_all(self
, limit
=None):
24 query
= "SELECT * FROM user_messages ORDER BY time_added ASC"
26 query
+= " LIMIT %d" % limit
28 return self
.db
.query(query
)
32 ret
= self
.db
.get("SELECT COUNT(*) as count FROM user_messages")
37 self
.db
.execute("DELETE FROM user_messages WHERE id = %s", id)
39 def send_to_all(self
, recipients
, subject
, body
, format
=None):
41 Sends an email to all recipients and does the translation.
46 for recipient
in recipients
:
48 logging
.warning("Ignoring empty recipient.")
51 # We try to get more information about the user from the database
53 user
= self
.pakfire
.users
.get_by_email(recipient
)
55 # Get locale that the user prefers.
56 locale
= tornado
.locale
.get(user
.locale
)
58 # Get the default locale.
59 locale
= tornado
.locale
.get()
61 # Translate the message.
62 _subject
= locale
.translate(subject
) % format
63 _body
= locale
.translate(body
) % format
65 # If we know the real name of the user we add the realname to
66 # the recipient field.
68 recipient
= "%s <%s>" % (user
.realname
, user
.email
)
70 # Add the message to the queue that it is sent.
71 self
.add(recipient
, _subject
, _body
)
76 logging
.warning("Dropping message with empty recipient.")
79 logging
.debug("Sending mail to %s: %s" % (msg
.to
, msg
.subject
))
81 # Preparing mail content.
82 mail
= MIMEText(msg
.text
.encode("latin-1"))
83 mail
["From"] = msg
.frm
.encode("latin-1")
84 mail
["To"] = msg
.to
.encode("latin-1")
85 mail
["Subject"] = msg
.subject
.encode("latin-1")
86 #mail["Content-type"] = "text/plain; charset=utf-8"
88 #smtp = smtplib.SMTP("localhost")
89 #smtp.sendmail(msg.frm, msg.to.split(", "), mail.as_string())
92 # We use sendmail here to workaround problems with the mailserver
94 # So, just call /usr/lib/sendmail, pipe the message in and see
95 # what sendmail tells us in return.
96 sendmail
= ["/usr/lib/sendmail", "-t"]
97 p
= subprocess
.Popen(sendmail
, bufsize
=0, close_fds
=True,
98 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
)
100 stdout
, stderr
= p
.communicate(mail
.as_string())
102 # Wait until sendmail has finished.
106 raise Exception, "Could not send mail: %s" % stderr