Have an idea?

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

Mail send twice via Perl

Dear Sawtooth-Team,

I am about to work on a survey which has the following requirements:

The participant needs to read and agree to certain terms and conditions during the survey.
As soon as the participant agrees, a cofirmation email should be send to his or her account.

Perl sends the Mail, but most of the time it sends the Mail twice.
 Can you maybe help me and tell my why this happens?

In case its important, the Mail is send from a Free-format question which is not visible for the participant.

Here is what i scripted:

[% Begin Unverified Perl
#!/usr/bin/perl
use MIME::Lite;
use utf8;



# Details for email participant
my $to = GETVALUE("Mail");
my $from = 'sender@abc.com';
my $subject = 'Subject of the mail';

no utf8;
my $type = "text/html";

# message
my $message = 'This is the message, including html formatting';


#mail to participant
my $msg = MIME::Lite->new(
    From=> $from, To=> $to, Subject =>  $subject, Type=> $type, Data=> $message);
    
$msg->send;

return "";
End Unverified %]



Best regards!
asked Jan 12 by Sarah

1 Answer

0 votes
I've ran into this issue as well.  It's a little hacky, but the workaround I found was to add a skip to the question and move the code into the skip logic instead of a normal question text field.  If we set the skip to "pre-skip" and end the code with "return 0;", the skip should never actually happen and the code should run only once on page load.  Remember that the "[%" and "%]" are not required in skip logic.

Begin Unverified Perl
# send email...
return 0;
End Unverified
answered Jan 12 by Zachary Platinum Sawtooth Software, Inc. (214,575 points)
Thank you- thats the perfect work around!
This saved me a lot of additional work :)

Do you know by chance, if I can put any PERL-script, like for example database queries, into a Pre-skip to make 100% sure, they are just executed once?

Best regards!
Good question.  This skip trick has seemed to work reliably for me, but would still trigger multiple times if the respondent were to go to the page a second time, either via skips or by backing up to that page.  If we need the script to only be able to run once per respondent, we could add a variable to our survey, set it when our code runs, and only run the code if it hasn't been set yet.  Assuming we name the variable "singleExecution", this should work:

[% Begin Unverified Perl
if (!GETVALUE('singleExecution')) {
    SETVALUE('singleExecution', 1);
    # do work...
}
End Unverified %]


Now, if you wanted the code to run only once EVER - even across multiple respondents - that would take a little more work.
Thats a very good idea and more then enough for me. I think I will try this code next time.

Thanks again for your help!

Best regards.
Sorry for comming back to this issue, but do you know by chance why the code doesnt show letters like "ä" or "ß" (utf8) in the subject?

Is there any code i need to modify?
I don't have an email server to test with, but I can try to take a shot in the dark or two.  What are you seeing in the received email when these characters are used?  Are these characters just being used in a literal string in your Perl or is there any new code involving the email subject that hasn't been included in your original code block?
No, there is no new code included. "ä" for example is shown as "Ã□". The funny part is, if i delete line 13 (no utf8), then the subject works fine, but the text in the mail doesnt show those signs like "ä,ü,ö" etc anymore.

Best refards
That's odd.  I'm not sure what is happening here - I don't have a lot of experience with MIME or character encoding in Perl.  I've wrapped in a coworker who may be better equipped for this.

If you haven't already, naturally I'd recommend to keep experimenting to see if we can manually get a better understanding of how the code is working.  What happens if we remove lines 4 and 13?  Or what if we call the encode/decode methods on utf8 (https://perldoc.perl.org/utf8)?
...