Replying to an incoming email message

Discussion in 'Perl' started by hansadamsson, Jan 16, 2010.

  1. hansadamsson

    hansadamsson New Member

    Joined:
    Jan 16, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello!

    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(at)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
    forums/showthread.php?p=30502
    ii) Sending Mails using Sendmail
    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 script that can accomplish the task postulated above. Many thanks in advance.

    Best regards,
    Hans Adamsson
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Do you want help to write the script or do you want someone to write the script for you?? CPAN has many module which can parse emails for you, one very good module is MIME::Parser
     
  3. hansadamsson

    hansadamsson New Member

    Joined:
    Jan 16, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi Pradeep,

    I am still new to Perl, nevertheless I am trying to learn fast; I have now acquired a lot of tutorials/books on Perl-programming.

    It will be highly appreciated if you will write the script for me. I am sure that you know what I will like to accomplish as postulated in my previous message. My site is UNIX-based.

    Regards,
    Hans Adamssson
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    #!/usr/bin/perl
    
    use Net::POP3;
    use MIME::Parser;
    
    my $pop = Net::POP3->new('localhost', Timeout => 60);
    my $p = new MIME::Parser;
    
    if ($pop->login('your-username', 'your-password') > 0) 
    {
    	my ($num,$size) = $pop->popstat;
    	
    	for(my $i=1;$i<10;$i++)
    	{
    		my $m = join('',@{$pop->get($i)});
    		my $mo = $p->parse_data($m);
    
    		if($mo->head->get('Subject') =~ /your reqd pattern/)
    		{
    		   my $msg = MIME::Lite->new(
    				From    => 'hans@hans.com',
    				To      => $mo->head->get('From'),
    				Subject => "Reply",
    				Type    => 'text/plain',
    				Data	=> q[Hello, how are you?]
    			);
    
    			open( MAIL, "|/usr/sbin/sendmail -t" ) or warn($!);
    			print MAIL $msg->as_string;
    			close MAIL;
    		}
    
    		$pop->delete($i);
    	}
    }
    
    $pop->quit;
    
     
    Last edited: Jan 30, 2010
  5. hansadamsson

    hansadamsson New Member

    Joined:
    Jan 16, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello Pradeep,

    The script is not yet working. I make some modification to the script to reflect my website parameters :)See the attached script). After that I transfered (in ASCII mode) the script (named indigos.cgi), POP3.pm and Parser.pm into the cgi directory on my website. I then chmod(UNIX)755 all the files.

    I sent a message with header containing the expression "ultimate brochure" to info@mywebsite.com. The message was sent from another email address from another webmail. Unfortunately I did not get any respondse-reply from info(at)mywebsite despite "ultimate brochure" in the header's line.

    Is there any issue I am overlooking? Any help and enlighnment will be highly appreciated. Many thanks in advance.

    Regards
    Hans

    Code:
    #!/usr/bin/perl
    
    use Net::POP3;
    use MIME::Parser;
    
    my $subscribe_subject = "ultimate brochure";  #added
    
    my $pop = Net::POP3->new('mail.mywebsite.com', Timeout => 60); #modified
    my $p = new MIME::Parser;
    
    if ($pop->login('info@mywebsite.com', 'mysecretpassword') > 0) 
    {
        my ($num,$size) = $pop->popstat;
        
        for(my $i=1;$i<10;$i++)
        {
            my $m = join('',@{$pop->get($i)});
            my $mo = $p->parse_data($m);
    
            if($mo->head->get('Subject') =~ /$subscribe_subject/)  #changed
            {
               my $msg = MIME::Lite->new(
                    From    => 'info@mywebsite.com',
                    To      => $mo->head->get('From'),
                    Subject => "Reply",
                    Type    => 'text/plain',
                    Data    => q[Hello, how are you?]
                );
    
                open( MAIL, "|/usr/sbin/sendmail -t" ) or warn($!);
                print MAIL $msg->as_string;
                close MAIL;
            }
    
            $pop->delete($i);
        }
    }
    
    $pop->quit;
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice