www.webdeveloper.com
Results 1 to 12 of 12

Thread: Autoresponder with headerís sensitivity

  1. #1
    Join Date
    Sep 2009
    Posts
    10

    Wink Autoresponder with headerís sensitivity

    Hello!

    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! #
    #################################

    use CGI qw/:standard :cgi-lib/;

    @date=localtime();
    $date[4]++;
    $date[5]+=1900;
    $date="$date[5]-$date[4]-$date[3]";

    $scount = 0;
    $uscount = 0;
    $dcount = 0;
    %messages = ();
    $messages = $pop->list;
    foreach $msgid (keys %$messages) {
    $mymessage = $pop->get($msgid);
    $to = "";
    $from = "";
    $subject = "";
    if($mymessage) {
    @mess = @$mymessage;
    foreach $line (@mess) {
    if ($line =~ m/To: /) {
    $line =~ s/To: // unless ($line =~ m/Reply-To:/i);
    $line =~ s/<//gm;
    $line =~ s/>//gm;
    chomp($line);
    $to = $line;
    }
    if ($line =~ m/From: /) {
    $line =~ s/From: //;
    $line =~ s/<//gm;
    $line =~ s/>//gm;
    chomp($line);
    @lines = split(/ /,$line);
    foreach $line2(@lines) {
    if($line2 =~ /\@/) { $from = $line2; }
    }
    }
    if($line =~ m/Subject: /) {
    $line =~ s/Subject: //;
    chomp($line);
    $subject = $line;
    }
    } #done message scan
    $mfound = 0;
    $to =~ tr/A-Z/a-z/;
    $from =~ tr/A-Z/a-z/;
    if($to eq $subscribe_email) { &subscribe; }
    }
    }



    sub error
    {
    my $error = shift;
    print <<EOF;
    <title>Error</title>
    Error: $error
    EOF
    exit;
    }

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    Please use the [CODE] tags around your code to preserve formatting -- this unindented code is very hard to read.

  3. #3
    Join Date
    Sep 2009
    Posts
    10

    Wink Autoresponder with headerís sensitivity

    Code:
    #!/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!      #
    ####################################
    
    use CGI qw/:standard :cgi-lib/;
    
    @date=localtime();
    $date[4]++;
    $date[5]+=1900;
    $date="$date[5]-$date[4]-$date[3]";
    
    $scount = 0;
    $uscount = 0;
    $dcount = 0;
    %messages = ();
    $messages = $pop->list;
    foreach $msgid (keys %$messages) {
    $mymessage = $pop->get($msgid);
    $to = "";
    $from = "";
    $subject = "";
    	if($mymessage) {
    	@mess = @$mymessage;
    		foreach $line (@mess) {
    			if ($line =~ m/To: /) {
    			$line =~ s/To: // unless ($line =~ m/Reply-To:/i);
    			$line =~ s/<//gm;
    			$line =~ s/>//gm;
    			chomp($line);
    			$to = $line;
    			}
    			if ($line =~ m/From: /) {
    			$line =~ s/From: //;
    			$line =~ s/<//gm;
    			$line =~ s/>//gm;
    			chomp($line);
    			@lines = split(/ /,$line);
    				foreach $line2(@lines) {
    					if($line2 =~ /\@/) { $from = $line2; }
    				}
    			}
    			if($line =~ m/Subject: /) {
    			$line =~ s/Subject: //;
    			chomp($line);
    			$subject = $line;
    			}
    		} #done message scan
    		$mfound = 0;
    		$to =~ tr/A-Z/a-z/;
    		$from =~ tr/A-Z/a-z/;
    		if($to eq $subscribe_email) { &subscribe; }
    	}
    }
    
    
    
    sub error 
    {
    my $error = shift;
    print <<EOF;
    <title>Error</title>
    Error: $error
    EOF
    exit;
    }

  4. #4
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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.

    Have fun!

  5. #5
    Join Date
    Sep 2009
    Posts
    10

    Autoresponder with header's sensitivity

    Hi Sixtease,

    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.

    Best regards,
    Hans Adamsson

  6. #6
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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?
    Last edited by Sixtease; 01-01-2010 at 10:53 AM.

  7. #7
    Join Date
    Sep 2009
    Posts
    10

    Red face Autoresponder with header's sensitivity

    Hi Sixtease,

    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.

    Best regards,
    Hans Adamsson, Finland

  8. #8
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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.

    If you want to start learning Perl itself, I found an interesting resource list here: Where and how to start learning Perl. I myself found official Perl documentation the best resource for learning 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?

  9. #9
    Join Date
    Sep 2009
    Posts
    10

    Unhappy Autoresponder with header's sensitivity

    Hello Sixtease,

    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.

    Best regards,
    Hans

  10. #10
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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.

  11. #11
    Join Date
    Sep 2009
    Posts
    10

    Red face

    Hello!

    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.

    I am not an expert in CGI/Perl, nevertheless I am trying to learn fast. After searching the internet for the past three weeks I came about these articles:
    i) Accessing Mail via POP3 in Perl & PHP
    http://www.go4expert.com/forums/showthread.php?p=30502
    ii) Sending Mails using Sendmail
    http://www.go4expert.com/forums/showthread.php?t=13133

    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.

    Best regards,
    Hans Adamsson

  12. #12
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    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();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles