Undeclared dependency on mailx

Bug #137994 reported by John Edwards
4
Affects Status Importance Assigned to Milestone
unattended-upgrades (Ubuntu)
Fix Released
Undecided
Michael Vogt

Bug Description

Binary package hint: unattended-upgrades

When asked to send an email report the unattended-upgrade python script uses the 'mail' command without checking that it is present. There is also no dependency on the mailx package, so it will not be installed automatically.

Below is a patch I've written that uses python's own smtplib module to send the email as well as adding a few extra bits of debugging output:

--- unattended-upgrade 2007-09-05 16:34:59.000000000 +0100
+++ unattended-upgrade_smtplib 2007-09-05 17:34:55.000000000 +0100
@@ -15,6 +15,7 @@
 import apt
 import logging
 import subprocess
+import smtplib

 class MyCache(apt.Cache):
     def __init__(self):
@@ -256,14 +257,14 @@
     # check if we need to send a mail
     email = apt_pkg.Config.Find("Unattended-Upgrade::Mail", "")
     if email != "":
- logging.debug("Sending mail with '%s' to '%s'" % (logfile_dpkg, email))
- mail = subprocess.Popen(["mail",
- "-s","unattended-upgrades result",email],
- stdin=subprocess.PIPE)
- s = "Unattended upgrade returned: %s\n\n" % (res != pm.ResultFailed)
- s += open(logfile_dpkg).read()
- mail.stdin.write(s)
- mail.stdin.close()
- ret = mail.wait()
- logging.debug("mail returned: %s" % ret)
-
+ logging.debug("Sending mail using smtplib with '%s' to '%s'" % (logfile_dpkg, email))
+ msg = ("From: %s\r\nTo: %s\r\nSubject: unattended-upgrades result\r\n\r\n" % ("unattended-upgrades", email))
+ msg += "Unattended upgrade returned: %s\n\n" % (res != pm.ResultFailed)
+ msg += open(logfile_dpkg).read()
+ logging.debug("Content of email should be '%s'" % (msg))
+ server = smtplib.SMTP('localhost')
+ if options.debug:
+ server.set_debuglevel(1)
+ server.sendmail("unattended-upgrades", email, msg)
+ server.quit()
+

Revision history for this message
John Edwards (john-cornerstonelinux) wrote :

There is another undeclared dependency here - having a mail server on the localhost. This may not always be wanted.

The patch could be changed by adding another configuration variable for the mail server, which would default to localhost if not set.

Revision history for this message
John Edwards (john-cornerstonelinux) wrote : Patch to use smtplib plus a new Mail-Server setting

This patch adds the above code for smtplib instead of using the mail binary, but also adds a new Unattended-Upgrade::Mail-Server setting for which SMTP server to connect to.

This is allows people to use an external SMTP server and not have to run a full mail server on localhost.

Revision history for this message
John Edwards (john-cornerstonelinux) wrote : Patch to config file for new Unattended-Upgrade::Mail-Server setting

This patch to /etc/apt/apt.conf.d/50unattended-upgrades adds an example of the new Unattended-Upgrade::Mail-Server setting, as well as including the expanded comments and examples from my patch to Bug #136452:
    https://bugs.launchpad.net/ubuntu/+source/unattended-upgrades/+bug/136452/comments/3

Revision history for this message
Michael Vogt (mvo) wrote :

Thanks for your bugreport and your patch.

I added more documentation about the need for the mailx package and added it to the "Suggests" in debian/control and write a propper error into the log if mailx is not available.

The reason why I didn't include your patch is that I would rather depend on a properly configured local mailsystem than on an additional configuration for the smtp server. Local working mail is required on a server for stuff like cron mails anyway, so I think its ok to assume that.

If you think that the smtplib based sending code is still useful for certain setups, we can still include it and use it if no /usr/bin/mail is available. Please let me know what you think!

Thanks,
 Michael

Changed in unattended-upgrades:
assignee: nobody → mvo
status: New → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package unattended-upgrades - 0.30ubuntu1

---------------
unattended-upgrades (0.30ubuntu1) hardy; urgency=low

  [ John Edwards ]
  * fix missing spaces in the log file (LP: #136452)
  * add better comments and examples to data/50unattended-upgrades

  [ Michael Vogt ]
  * README improved based on Johns suggestions (thanks!)
  * fix grammer and dated reference to dapper (LP: #140038),
    thanks to Christer Edwards for the suggestions
  * add "mailx" to suggests (LP: #137994)
  * show propper log output if mail is not available (LP: #137994)
  * show packages that are held back from the upgrade because of
    conffile prompt in the log as a warning (LP: #133551)
  * create logdir if it does not exist (LP: #87338)
  * fix error when installing from file:// uris (LP: #56832)

 -- Michael Vogt <email address hidden> Mon, 10 Mar 2008 11:57:17 +0100

Changed in unattended-upgrades:
status: Fix Committed → Fix Released
Revision history for this message
John Edwards (john-cornerstonelinux) wrote :

Hi Michael
Thanks for your work on this package.

For most cases the second patch doesn't need any changes to the configuration file as it defaults to port 25 on localhost.

Where my patch would need a configuration change is where the mail system does not listen on port 25 and relies on delivery via a sendmail binary. Examples would be a satellite MTA which just passes email onto another system, examples being sstmp or a special configuration of a full MTA such as postfix/exim/sendmail. These systems would need the Mail-Server variable set in the configuration file.

If the mail server requires the use of TLS and certificates then smtplib could cover that, but I haven't written any code yet.

Advantages of continuing to use mailx:
1) Doesn't required an MTA that listens on port 25.
2) Does not extra code to work with MTAs that require TLS.

Advantages of using smtplib:
1) Better options for logging and debugging.
2) No reliance on mailx package or subprocesses.
3) No reliance on working MTA on localhost.

If we wish to include both than I could add a little extra code to see /usr/bin/mail is present and if it is not then to switch to using smtplib.

I'll leave it to you to decide which is the best of those three options, especially considering the upcoming release next month.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.