如何将主题行包含在perl电子邮件脚本中?

时间:2013-12-18 19:08:48

标签: perl

我是perl的新手,如何在下面的代码中将电子邮件显示在电子邮件中?现在它只发送不包含主题行的电子邮件。

收件人会收到一封电子邮件,具体取决于与其电子邮件帐户相匹配的邮政编码,如下面的代码所示。

此外,当它发送包含数据的电子邮件时,电子邮件@符号无法正常显示。我该如何解决这个问题?

由于

   #!/usr/bin/perl

print "Content-type: text/html\n\n";
%form=&parse_form();
$other_email=&lookup($form{'zipcode'});
$subject = 'Test Email';
$body=
"Customer Name: $form{'name'}\n".
"Customer Phone: $form{'phone'}\n".
"Customer Email: $form{'email'}\n".
"Customer Zip: $form{'zipcode'}\n";

mailsend('myemail@gmail.com',$body) and print "<b>Thank You!</b><br>\n";
mailsend($form{'email'},$body) and print "<b>Your email has been sent.</b><br>\n";
mailsend($other_email,$body) and print "<b>Please hit back to exit this page.</b><br>\n";

#helper functions
sub lookup {
    $contents=`cat postcodes.txt`;
    ($result)=$contents=~m/\b$_[0]=(.*)\b/;
    print "<b>Corresponding email address  for zip $_[0] was [$result]</b><br>\n";
    return $result;
}

sub mailsubject {
$subject = 'Test Email';
}

sub mailsend {
    open(MAIL, "|/usr/sbin/sendmail -t");
    print MAIL "To: $_[0]\n\n";
    print MAIL "See email below!\n\n";
    print MAIL "$_[1]\n";
    print MAIL "Thank you\n";
    close(MAIL);
    return true;
}

sub parse_form {
    ($hash{'name'}) = $ENV{'QUERY_STRING'}=~/name=(.*?)&/;
    ($hash{'phone'}) = $ENV{'QUERY_STRING'}=~/phone=(.*?)&/;
    ($hash{'email'}) = $ENV{'QUERY_STRING'}=~/email=(.*?)&/;
    ($hash{'zipcode'}) = $ENV{'QUERY_STRING'}=~/zipcode=(.*?)&/;
    return %hash;
}

2 个答案:

答案 0 :(得分:3)

您只需为主题添加其他标题:

sub mailsend {
    my ($recip, $subject, $body) = @_;  # <- extract into variables

    open(MAIL, "|/usr/sbin/sendmail -t");
    print MAIL "To: $recip\n";
    print MAIL "Subject: $subject\n";   # <- added Subject line
    print MAIL "\n";                    # end of headers
    print MAIL "See email below!\n\n";
    print MAIL "$body\n";
    print MAIL "Thank you\n";
    close(MAIL);

    return true;
}

这似乎是一项额外的工作,因为CPAN上已经有一个Perl模块以更简单的方式处理电子邮件:MIME:Lite

答案 1 :(得分:1)

如果您能够使用外部模块,使用Email :: Simple和Email :: Sender将允许您不需要重新实现轮子:

use strict;
use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Simple::Creator;

sub mailsend {
    my ($to, $subject, $body) = @_;
    my $email = Email::Simple->create(
        header => [
            To      => $to,
            From    => '"My Script" <myscript@example.com>',
            Subject => $subject,
        ],
        body => $body,
      );
    sendmail($email);
}