#! /usr/local/bin/perl # Mail2Fax by Hermelito Go, lito@msuiit.edu.ph 6/2/1997 # URL: http://www.msuiit.edu.ph/~lito/mail2fax # This mail2fax script is copyrighted by Hermelito Go. You are free to # distribute this script for free, if you made any changes please let me # know and you are bound to agree that the changes you made maybe # incorporated in its next incarnation. There is no warranty whatsoever # that this script will work on your system, however this system have been # tested under linux. If you can run mgetty+sendfax, perl and sendmail this # script should also work. # HOWTO install # 1. install perl into your system (www.perl.org) # /usr/local/bin/perl should exist after installation if not make # a copy or a symbolic link # # 2. install mgetty+sendfax by Gert Doering # (http://www.leo.org/~doering/mgetty/index.html) # Test it first that mgetty and sendfax works with your modem. # # 3. If you are not running sendmail you may alter the $mailer # variable to suit to your MTA. # # add this lines in your /etc/aliases # # faxadmin : root # fax : "|/usr/local/etc/mail2fax fax@host.yourdomain faxadmin" # # run following commands: # # cp mail2fax /usr/local/etc # touch /var/spool/fax/maillog # # run "sendmail -bi" to reinitialize your mail aliases # # 4. Schedule "faxrunq" to suit to your needs. # # 5. Your mail2fax server is now ready for service. # Modify $replies and $mailer for your system if necessary: $mailer = "/usr/lib/sendmail -f$ARGV[1] -oi"; $* = 1; # Read header into the variables $from (for reply) # and $header (for the rest of the header) while () { last if /^$/; # Make key lower-case: if (/^From /) { @from = (split(/^([-\w]+)[ ]*/)); } elsif (/^\S/) { @part = (split(/^([-\w]+:?)[ ]*/)); $part[1] =~ tr/A-Z/a-z/; $header .= "$part[1] $part[2]"; } else { $header .= $_; } if (/^Subject/) { $subject .= $_; } } # Note: loses multiple lines with same key (like Received:): $header =~ s/\n\s+/ /g; # Merge continuation lines. $subject =~ s/\n\s+/ /g; # Merge continuation lines. chop $subject; ($subj, $phone, $rest) = split(/:./,$subject,2); %head = ('FRONTSTUFF', split(/^([-\w]+:?)[ ]*/, $header)); # Get address to use: if (defined($head{'reply-to:'})) { $useaddr = $head{'reply-to:'}; } elsif (defined($head{'from:'})) { $useaddr = $head{'from:'}; } elsif (defined($head{'apparently-from:'})) { $useaddr = $head{'apparently-from:'}; } chop $useaddr; # The following lines are local customization for my fax server at # MSU-IIT. You may erase it or provide your own local preferences. # Check if phone number was provided in the subject line. if ( $phone eq "" ) { open(MAIL, "|$mailer -t") || die; print MAIL "To: $useaddr\n"; print MAIL "Subject: MSU-IIT Internet FAX: ERROR"; print MAIL "\n\nYou did not provide a proper FAX number on the Subject:\n"; print MAIL "\n For instructions check http://www.msuiit.edu.ph/fax.html\n"; close(MAIL); exit(0); } # Check for Iligan telephone prefix, reject all others. if (!($phone =~ /^221/)) { open(MAIL, "|$mailer -t") || die; print MAIL "To: $useaddr\n"; print MAIL "Subject: MSU-IIT Internet FAX: ERROR"; print MAIL "\nNew Iligan Telephone prefix is 221.\n"; close(MAIL); exit(0); } # (Subject line) Fax number should only contain numerals. if ($phone =~ /[a..zA..Z]/) { open(MAIL, "|$mailer -t") || die; print MAIL "To: $useaddr\n"; print MAIL "Subject: MSU-IIT Internet FAX: ERROR"; print MAIL "\nThe subject line should only contain the numeric FAX Number\n"; print MAIL "\n For instructions check http://www.msuiit.edu.ph/fax.html\n"; close(MAIL); exit(0); } # Check for e-mail address, else bounce. if (defined($useaddr)) { open(LOG,">>/var/spool/fax/maillog"); print LOG "$useaddr $phone "; close (LOG); system "date >>/var/spool/fax/maillog"; open(MAIL, "|$mailer -t") || die; print MAIL "To: $useaddr\n"; print MAIL "Subject: MSU-IIT Internet FAX: $phone\n"; print MAIL "\n\n ----------- Your original message is below ----------\n\n"; open(FAX,"|/usr/local/bin/faxspool -n -f '$useaddr' '$phone' -") || die; print FAX "\nA community service provided by MSU-IIT, Computer Center\n"; print FAX "\n\n$header"; while () { print FAX $_; print MAIL $_; } close(FAX); close(MAIL); } else { open(MAIL, "|$mailer $ARGV[1]") || die; print MAIL "$0 Fax aborted: can't find address to reply to.\n\n"; print MAIL "This message needs a reply:\n\n>"; print MAIL ; close(MAIL); } exit(0); # exit gracefully