Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

How to send automatic email notification to me if a question is answered

Hi, I'd like to receive an automatic email notification if a respondent answers a specific question (there should be no action taken by the user, I am not interested in learning his/her email adress).
Many thanks for your hints, Thomas
asked Feb 27, 2012 by thagel (255 points)
retagged Sep 1, 2016 by Walter Williams
Glad it could help.  I have updated my code to reflect your suggestions as well

1 Answer

+2 votes
This is quite a complex feature to achieve.

This can be done a few different ways but all involve the server where you will be collecting the data to be configured for this.

If the data collection server is configured to also function as a mail server then this should be pretty straightforward using Unverified Perl.  On linux/unix it could look like this (make sure this path matches for your sendmail program).

[% 
Begin Unverified Perl

my $sendmail = "/usr/sbin/sendmail -t";
my $subject  = "Subject: User saw question X\n";
my $content  = "Respondent #" . RESPNUM() . " saw question X";
my $send_to  = "To: myemailaddress\@something.com";

open(SENDMAIL, "|$sendmail") or return "Cannot open $sendmail: $!";
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);

return "";


End Unverified
%]


I would put this code on a page that had quotas for example since they are never seen but the logic is run.  The quota could just be garbage data, but nonetheless would work.

Sending from windows servers is more complex.  Using Activestate perl, install the Mail::SendMail perl package.  If this package is installed on your linux server then you can use this code as well.  Your unverified perl code could look like this.

[% 
Begin Unverified Perl

require "Mail/SendMail.pm";

my %mail = ( To      => 'you@there.com',
            From    => 'me@here.com',
            Message => "This is a very short message"
           );

Mail::SendMail->sendmail(%mail);

return "";

End Unverified
%]
answered Feb 27, 2012 by Mike Lodder Gold (23,395 points)
edited Mar 1, 2012 by Mike Lodder
Hi Mike, many thanks, that was very helpful.
I just had to add "\" before the "@" because I got a script error. Apparently Perl was expecting an array. I also added "send_from" so - for the record - the final thing that worked looked like this:

[%
Begin Unverified Perl
 my $sendmail = "/usr/sbin/sendmail -t";
my $subject  = "Subject: User saw question X\n";
my $content  = "Respondent #" . RESPNUM() . " saw question X\n";
my $send_to  = "name\@something.com";
my $send_from = "webmaster\@something.com";
 
open(SENDMAIL, "|$sendmail") or return "Cannot open $sendmail: $!";

print SENDMAIL "To: $send_to\n";
print SENDMAIL "From: $send_from\n";
print SENDMAIL $subject;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);
 
return "";
 End Unverified
%]
...