HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   perl script hylafax email to fax gateway (http://www.howtoforge.com/forums/showthread.php?t=11595)

nenad 23rd March 2007 08:26

perl script hylafax email to fax gateway
 
Hello to everyone,

I am setting up hylafax server, and everything is working except one detail:

I have some problems with following script:

Code:

#!/usr/bin/perl
use MIME::Parser;
use POSIX;

my $tmp = "/tmp/mail-$$";
mkdir $tmp, 0700;
my $log_file = "/var/log/faxgw";

my $parser = new MIME::Parser;
$parser->output_under($tmp);

my $entity = $parser->parse(\*STDIN);

my @parts;
my @entities = ($entity);

while ($_ = shift(@entities))
{
    my @p;
    if (@p = $_->parts)
    {
        unshift @entities, @p;
    } else
    {
        push @parts, $_;
    }
}

my $from = $entity->head->get('From');$from =~ s/\n//go;
my $oldsubject = $entity->head->get('Subject');$oldsubject =~ s/\[(\d+)\]\s*(.*)//go;$number = $1;
my $subject = $2;

system( "sendfax",
        "-R",
        "-m",
        "-n",
        "-f", $from,
        "-r", $subject,
        "-d", $number,
        map ({$_->bodyhandle->path() } @parts)
);

#system("/bin/rm", "-rf", $tmp);

my $logmessage = strftime("%d.%m.%Y %H:%M:%S", localtime) . " Sent fax, from: $from, to: $number, regarding: $subject.\n";
open (my $log, ">>", $log_file) || die "Could not open $file for append: $!";
print $log $logmessage;
close $log;

This script is doing following:

strips out from email:
- text component
- html component
- attachements

it creates in /tmp/ directory

mail-16738 sub-directory and within it it creates

msg-1174598017-16738-0 subdirectory and within it creates files

msg-16738-1.txt msg-16738-2.html TestPage.ps

I think it's done somewhere within loop:

while ($_ = shift(@entities)) ...etc )

and the script is invoking system command sendfax to send a fax.

What I need is limiting to extracting only attachement component out of email, or parsing just a attachement to sendfax, withouth text and html components.

Considering that I don't have any knowledge about perl , please HELP !!!

nenad 23rd March 2007 19:12

One of the Developers of Hylafax sent to my email solution to this:

Quote:

> while ($_ = shift(@entities))
> {
> my @p;
> if (@p = $_->parts)
> {
> unshift @entities, @p;
> } else
> {
> push @parts, $_;
> }
> }

> What I need is limiting to extracting only attachement component out of
> email, or parsing just a attachement to sendfax, withouth text and html
> components.

You have to decide "how" you will determine if a part is given to
sendfax. That will depend on what types of emails you feed into it, and
what results you want.

It could be as simple as changing the one line.

If you want to only fax the PostScript documents:

push @parts, $_ if $_->mime_type eq 'applicatin/postscript';

or if you want to fax everything *except* text/plain or text/html:
next if $_->mime_type eq 'text/plain';
next if $_->mime_type eq 'text/html';
push @parts, $_;


All times are GMT +2. The time now is 05:55.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.