I am trying to code a script for subscription, but I need some help. Just like a program that is an online vacation reply manager with certain exception. In other words, when someone sends an email to the auto-responder, a follow-up begins depending on the content of the header.
My script will be located in cgi-bin directory on my site. The function of the script will be to check the content of header of incoming email. If subject of the incoming-email contains the words “ultimate brochure ” (case insensitive), the script sends a one-time follow-up email to the subscriber with message in “brochure.txt”. Let us assume that the sender/subscriber of an email to me put a heading like “I will like to subscribe for ultimate brochure”, the cgi-script must be able to scan the heading to see whether “ultimate” and “brochure” are among the words in the heading. Follow-up email is not sent if both “ultimate” and “brochure” words are not within the content of the heading.
My initial code is as below. It will be highly appreciated if somebody can put me through (in the form of coding) in this my endeavour. I thank you very much in advance for your help and enlightenment. hans.adamsson(at)wippies(dot)fi
Best regards,
Hans Adamsson, Finland
NB:
Script file: request.cgi
------------------------
#!/usr/bin/perl
################################
$script_name = 'request.cgi';
$from = 'info@mywebsite.net';
################################
# Path to Sendmail on my server.
$mailprog = '/usr/sbin/sendmail';
##################################
# Advanced Settings Auto Responder
$auto_responder_from = 'info@mywebsite.net';
$auto_responder_message = "brochure.txt";
##################################
# Subscribe Condition: Subject of email MUST contain the following words: ultimate brochure (case insensitive).
$subscribe_subject = "ultimate brochure";
#################################
# MAIN CODING FOR THE SCRIPT! #
#################################
How about using a module? You need not write your own email parser. And how about using strict? :-)
I've never had to work with CGI so I don't really understand the $pop thingy. I'll just assume you can traverse the email messages one by one as you do in your script.
Code:
#!/usr/bin/perl
use strict;
use Email::Simple;
use Email::Reply; # will let us reply()
use Email::Sender::Simple qw(sendmail);
my $from = 'info@mywebsite.net';
my $auto_responder_from = 'info@mywebsite.net';
my $auto_responder_message = "brochure.txt";
my $subscribe_subject = "ultimate brochure";
# easier way to get "2009-12-31"
use Time::Piece;
my $date = localtime->ymd;
my $autoreply_body = do {
open my $brochure_fh, '<', $auto_responder_message
or error("Could not open autoresponder message: $!");
local $/; # set slurp mode
<$brochure_fh> # read the file
};
for my $message (get_list_of_messages) {
my $inmail = Email::Simple->new($message);
if ($inmail->header('Subject') =~ /$subscribe_subject/) {
# send auto-reply
my $outmail = reply(
to => $inmail,
from => $auto_responder_from,
body => $autoreply_body,
top_post => 1, # put response before quoted text
);
sendmail($outmail);
}
}
sub error {
my $error = shift;
print <<EOF;
<title>Error</title>
Error: $error
EOF
exit;
}
This is just a skeleton -- a guideline. You'll have to debug it and adapt for your environment.
The code is not working. I configured the script's code to my site information. Then I uploaded the script onto my website. I chmod the script (which I named "responder.pl") 755. I chmod "brochure.txt" 777. Then I sent email from another email-address to my site's email-address. I put "ultimate brochure" as the heading of the email. I did not get any reply from the "responder.pl" from my website.
Is there some code missing in the above code you supplied?
If I may explicitly restate what I will like to achieve, I am looking for an autoresponder - A script for receiving a mail and immediately replying, depending on content of the header.
My script will be residing in cgi-bin directory on my site. The function of the script will be to check the content of header of incoming email. If subject of the incoming-email contains the words “ultimate brochure” (case insensitive), the script sends a one-time follow-up email to the subscriber with message in “brochure.txt”. Follow-up email is not sent if both “ultimate” and “brochure” words are not within the content of the heading.
Again I thank you very much in advance for your help and enlightenment.
As I said, my code does not deal at all with getting the incoming email messages from the request.
I suggest you run the code on command line and debug it. You'll have to do some work yourself.
Hint: you can simply implement the get_list_of_messages function which I call in the code but haven't written.
Update: Oh and of course, you have to make sure you have the required modules installed.
And why exactly do you set the brochure.txt file as executable? Wouldn't 644 be more adequate permissions for it?
I am still an amateur and new writing Perl script. However, I am trying to learn fast. Please which program should I use to run the code on command line and debug it.
Ah, I thought you had written the initial code that you posted yourself. For editing the source code, use any text editor you like (preferably one that can highlight Perl syntax).
Executing the code and viewing the output can be done in different ways on different operating systems. In Linux and other Unix-like OS's, just run "perl responder.pl" in the directory where responder.pl resides.
If you use Windows, have a look at Strawberry Perl.
Now tell me, are you sure the code you had originally posted worked? I mean, did it scan and process the email messages? There is one part that looks suspicious to me, and that is the use of the never-declared $pop variable, which I found no mention of in CGI manual.
If it really works, then it looks to me (I really just guess here) that you could simply replace get_list_of_messages with values(%{ $pop->list })
Did you write it yourself? Where do the incoming email messages come from?
The initial code I posted was not written by me. Going through all the CGI/Perl codes I could found on the internet, I tried to formulate something towards my intended autoreponder. The expression { $pop->list} has no justification. The initial code did not work since I am new to programming in Perl language.
The incoming emails are supposed to come from subscribers any where around the world. Subscribers send emails to, say, info@mywebsite.net on my website. When the subscribers send emails to me, the “responder.pl” (in the cgi-bin directory on my website) processes the emails. The “responder.pl” must be able to check whether there is “ultimate brochure” in the content of the header. If the outcome is true, a one-time followup email is sent to the subscriber.
Please can you provide me with some solution/code about this “get_list_of_messages” function. I thank you very much in advance.
Hmm, well, then you'll have to configure your server to accept e-mail and pass them to the script, first. I'm afraid my knowledge does not suffice here.
For the past three weeks, I am still struggling to accomplish this task.
I need a Perl-script for unattended processing of incoming email message. The script will grab/get incoming email message via POP3 from the request and then process the incoming-message header. The script will reply to the message via Sendmail if and only if incoming-message contains matched strings in the subject line. The string may be a regular expression, (properly quoted and escaped).
In other words, when someone sends an email to info@mysite.net on my website, the script (located in cgi-bin directory) grabs the incoming email message. The task of the script will be to process the incoming-message header. If subject-line contains the words "ultimate brochure" (case insensitive), the script replies to the incoming message with a default-message.
My problem is how to write a code that processes the header of incoming email and checks whether the words/expression “ultimate brochure” are included; subsequently send reply.
It will be highly appreciated if somebody can provide me with a working script that can accomplish the task postulated above. Many thanks in advance.
Looks like you should be using the Net::POP3 module. So install it to your server first.
Start by making sure you can retieve incomming emails.
You can then deternmine where the 'ultimate brochure' appears and code accordingly.
Code:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp(qw/fatalsToBrowser/); ## Errors to browser
use Net::POP3;
my $q = CGI->new();
print $q->header(); ## Needed to send html to browser
my $pop3 = Net::POP3->new('mail.domain.com'); ## create a new instance of Net::POP3
if ($pop3->login('username', 'password')) { ## login
my $msg_numbers = $pop->list(); ## hashref of msg_numbers=>size
my $num = 0;
foreach my $msg_number (keys %$msg_numbers) {
my $msg = $pop->get($msg_number); ## arrayref of lines in message
print '<p>'.join("<br>",@$msg).'</p>';
$num++;
last if ($num>10); ## End loop after 10 messages
}
print '<p>Done</p>';
}
else {
print "<p>Login failed</p>";
}
$pop->quit();
Bookmarks