Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   Replying to an incoming email message (http://www.go4expert.com/forums/replying-incoming-email-message-t20684/)

hansadamsson 17Jan2010 03:51

Replying to an incoming email message
 
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

pradeep 29Jan2010 17:13

Re: Replying to an incoming email message
 
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

hansadamsson 29Jan2010 23:36

Re: Replying to an incoming email message
 
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

pradeep 30Jan2010 10:00

Re: Replying to an incoming email message
 
Code: Perl

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


hansadamsson 30Jan2010 19:51

Re: Replying to an incoming email message
 
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;



All times are GMT +5.5. The time now is 05:18.