Bugzilla and SMTP Authentication

The last few months I’ve been helping out in an effort to upgrade Shotokan Karate of America‘s membership management system. They’re rewriting the system from scratch, as the old system has grown to be unmaintainable. The code for the new system was about 85% complete when I signed up, so I offered to start testing the system while the sole developer working on the project finished up the code. There were no unit tests, so I figured that would be a good place to start. This naturally brought up the issue of how we would communicate problems and fixes, so I offered to install Bugzilla on my Linux box and suggested we use that.

It wasn’t until later that I found out that Bugzilla and SMTP Authentication do not get along (although I see that there is a fix in the pipeline). Bugzilla supports SMTP, but without authentication. It also supports sendmail, but my ISP isn’t too keen on people running their own mail servers, making the use of sendmail difficult. Since my ISP has a SMTP server for me to use, I figured I would use that. But, authentication is required on that server.

I toyed around with some ideas in an attempt to keep me from hacking the Bugzilla code. One involved parsing the data/mailer.testfile file (where mail is sent when the “testfile” mail option is set) for the To address and the email body, and sending the email via a custom script. But, that brought up some interesting race conditions if multiple users made a change that triggered an email at approximately the same time. We would need to periodically read that file to send the email, wiping out the contents afterward to avoid resending the same messages. If the timing was right, we could potentially wipe out a message that was never sent.

I took a look at the Bugzilla code, and found the module that was responsible for sending the email. The code was clean, and pretty easy to follow, even though I know very little perl. I found the point in the module where it calls out to the Mail Transport Agent to deliver the mail. I figured I could easily insert a call to a script to deliver the mail, using SMTP Authentication. That is exactly what I did. I modified BugMail.pm to include the “system” call below.

sub MessageToMTA {
    ....

    # --- Begin New Lines ---
    system('send-bugzilla-email.rb', $msg);
    # --- End New Lines ---

    $mailer->open($headers->header_hashref);
    print $mailer $body;
    $mailer->close;
}

$msg is a variable that contains the SMTP message to send. That message includes the To address, the subject of the email, and the body of the email. In other words, everything we would need to send the mail ourselves. I then wrote a ruby script to parse the message, and send the email. Here’s the script.

#!/usr/bin/ruby

require 'net/smtp'

from_address = 'username@my_isp.com'

# Break the message up into an array of strings
message_array = ARGV.first.split("\n")

# Pull out the To address and the subject
message_array[1] =~ /^To: (.*)$/
to_address = $1

message_array[2] =~ /^Subject: (.*)$/
subject = $1

# Delete some crap that we do not care about
4.times { |i| message_array.delete_at(0) }

# Combine what remains back into the body
body = message_array.join("\n")

message =  "From: Bugzilla <#{from_address}>\n"
message << "To: #{to_address}\n"
message << "Subject: #{subject}\n"
message << body

# Send the email
Net::SMTP.start('smtp.my_isp.com', 25, 'my_isp.com', 'username', 'password', :login) do |smtp|
  smtp.send_message message, from_address, to_address
end

File.open('/var/log/bugzilla_email.log', 'a+') do |file|
  file.puts("\n-----------------------------------------\n")
  file.puts(message)
end

Although not ideal, it does the job. And, if I do say so myself, it works quite well. I’ll be looking forward to upgrading to the new version of Bugzilla once it supports SMTP authentication. In the meantime, this will keep things rolling.

Be Sociable, Share!

    13 thoughts on “Bugzilla and SMTP Authentication

    1. Ahmed, the ruby script can live anywhere. On my system, I have it in my user account’s bin directory (/home/jwood/bin). But, you can put it anywhere you’d like. Just change the system call in the bugzilla code to point to the full path of the script.

      For example, if you script is in /foo/bar/send-bugzilla-email.rb, then change the bugzilla code to be:

      system(‘/foo/bar/send-bugzilla-email.rb’, $msg);

    2. Thanks for ur fast reply.
      I tried it but it seems that i’m facing a problem before reaching this part
      as i get this error (There was an error sending mail from ‘bugzilla-daemon@’ to ‘ro_coolhead@yahoo.com’:Couldn’t authenticate ‘bugzillamailsender@gmail.com:…’) and there is nothing written in the log file of the rb code

    3. found the problem for this part, but now getting
      the following:

      syntax error at Bugzilla/BugMail.pm line 701, near “…”
      Global symbol “$msg” requires explicit package name at Bugzilla/BugMail.pm line 703.
      Global symbol “$mailer” requires explicit package name at Bugzilla/BugMail.pm line 706.
      Global symbol “$headers” requires explicit package name at Bugzilla/BugMail.pm line 706.
      Global symbol “$mailer” requires explicit package name at Bugzilla/BugMail.pm line 707.
      Global symbol “$body” requires explicit package name at Bugzilla/BugMail.pm line 707.
      Global symbol “$mailer” requires explicit package name at Bugzilla/BugMail.pm line 708.
      Compilation failed in require at C:/Program Files/Bugzilla/bugzilla/createaccount.cgi line 35.
      BEGIN failed–compilation aborted at C:/Program Files/Bugzilla/bugzilla/createaccount.cgi line 35.

    4. Ahmed, let’s continue this via email, so we don’t fill up the comments debugging this. Please email your BugMail.pm file to john@johnpwood.net. We’ll post the final solution here in the comments once we figure it out.

    5. Hi,
      I try to install bugzilla 4.0 and use smtp to send the notice but hit the problem
      “The new value for smtp_username is invalid: SMTP Authentication is not available. Run checksetup.pl for more details.”
      is this the same with you?
      thank you

    6. Hi John,

      I have installed bugzilla 4.2.1 on centos 6.2 and it is working fine, but when I try to send create new account invitation it will show the following:

      A confirmation email has been sent containing a link to continue creating an account. The link will expire if an account is not created within 3 days.

      But the client/user is not receiving any bugzilla mail.

      Thanks in advance,
      Ravindra

    7. HI Ravindra,

      I haven’t used Bugzilla since I wrote this blog post in 2008. So, unfortunately, I can’t help.

      John

    Leave a Reply

    Your email address will not be published. Required fields are marked *