簡體   English   中英

如何在Perl電子郵件腳本中包含主題行?

[英]how to include subject line to perl email script?

我是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;
}

您只需要為主題添加一個附加標題:

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

如果您能夠使用外部模塊,則使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM