Sending email using Perl using sendmail
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 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.
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);
The output of hostname
includes a newline, so $from
contains a newline, so the Subject:
line appears after a pair of newlines, so it’s interpreted as being in the message body. Easy to fix:
chomp($hostname);
You may find a similar issue with date
.