使用Perl使用sendmail发送电子邮件 [英] Sending email using Perl using sendmail
问题描述
我正在按照此网站中的示例使用Perl发送电子邮件。代码如下所示:
I am following the example from this website to send an email using Perl. The code looks like so:
my $hostname = `hostname`;
my $this_day = `date`;
my $email = "i.h4d35\@gmail.com";
my $to = "$email";
my $from = "admin\@$hostname";
my $subject = "SCHEDULE COMPLETE - $this_day";
my $message = "Student schedule for today, completed for the following students: \n\n$names\n\nHave a nice day...";
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL $message;
close(MAIL);
邮件已发送,但主题出现在邮件正文中,并且电子邮件中没有主题。我该如何解决?
The mail gets sent but the subject appears in the body of the mail and the email has no subject. How do I fix this?
PS:我至今还没有开始使用 MIME :: Lite
PS: Have not gotten around to using MIME::Lite
yet as I am still learning this.
推荐答案
使用网站中的示例是一个坏主意。
Using examples from websites is a bad idea.
尤其任何指示您直接制作和发送低级格式的网站。
Especially any website that instructs you to craft and send low-level formats directly.
您不应手动实现以下任何格式:
You should not implement any of the following formats manually:
- 电子邮件
- HTML
- CSV
- IRC协议
- etc
- HTML
- CSV
- IRC Protocol
- etc
在许多网站只是简单地告诉您如何使用模块来实现这些任务的情况下,很多网站都无济于事。
Which lots of websites unhelpfully detail how to do, when they should simply be telling you how to achieve these tasks with a module.
这是一种更为简单的方法,使用Email :: Sender和Email :: Simple,这两种软件都是由从事电子邮件工作的人编写的高质量软件。
Here is a much more simple approach, using Email::Sender and Email::Simple, both quality pieces of software written by somebody who deals with Email for a living.
use strict;
use warnings;
my $hostname = `hostname`;
my $this_day = `date`;
use Email::Simple;
use Email::Simple::Creator;
use Email::Sender::Simple qw(sendmail);
my $email = Email::Simple->create(
header => [
From => "admin\@$hostname",
To => "i.h4d35\@gmail.com",
Subject => "SCHEDULE COMPLETE - $this_day",
],
body => "Student schedule for today, completed for the following students: \n\n$names\n\nHave a nice day..."
);
sendmail($email);
这篇关于使用Perl使用sendmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!