]>
git.ipfire.org Git - pbs.git/blob - src/buildservice/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 process_queue(self
):
40 # Get 10 messages at a time and send them one after the other
42 messages
= self
.get_all(limit
=10)
44 for message
in messages
:
45 self
.send_msg(message
)
49 def send_to_all(self
, recipients
, subject
, body
, format
=None):
51 Sends an email to all recipients and does the translation.
56 for recipient
in recipients
:
58 logging
.warning("Ignoring empty recipient.")
61 # We try to get more information about the user from the database
63 user
= self
.pakfire
.users
.get_by_email(recipient
)
65 # Get locale that the user prefers.
66 locale
= tornado
.locale
.get(user
.locale
)
68 # Get the default locale.
69 locale
= tornado
.locale
.get()
71 # Translate the message.
72 _subject
= locale
.translate(subject
) % format
73 _body
= locale
.translate(body
) % format
75 # If we know the real name of the user we add the realname to
76 # the recipient field.
78 recipient
= "%s <%s>" % (user
.realname
, user
.email
)
80 # Add the message to the queue that it is sent.
81 self
.add(recipient
, _subject
, _body
)
83 def send_msg(self
, msg
):
85 logging
.warning("Dropping message with empty recipient.")
88 logging
.debug("Sending mail to %s: %s" % (msg
.to
, msg
.subject
))
90 # Preparing mail content.
91 mail
= MIMEText(msg
.text
.encode("latin-1"))
92 mail
["From"] = msg
.frm
.encode("latin-1")
93 mail
["To"] = msg
.to
.encode("latin-1")
94 mail
["Subject"] = msg
.subject
.encode("latin-1")
95 #mail["Content-type"] = "text/plain; charset=utf-8"
97 #smtp = smtplib.SMTP("localhost")
98 #smtp.sendmail(msg.frm, msg.to.split(", "), mail.as_string())
101 # We use sendmail here to workaround problems with the mailserver
103 # So, just call /usr/lib/sendmail, pipe the message in and see
104 # what sendmail tells us in return.
105 sendmail
= ["/usr/lib/sendmail", "-t"]
106 p
= subprocess
.Popen(sendmail
, bufsize
=0, close_fds
=True,
107 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
)
109 stdout
, stderr
= p
.communicate(mail
.as_string())
111 # Wait until sendmail has finished.
115 raise Exception, "Could not send mail: %s" % stderr
117 # If everything was okay, we can delete the message in the database.