Simple mail from list
I've written a very simple message board for family and friends that resides in a protected directory. What I need to do is a quick mail out every time a message is posted - to all the people on a list.
I looked around, but couldn't find a nice simple mail list thing in Perl. They're all large programs like newsletters or massive bulk mail things. I don't really need all that for what I'm doing.
I doubt there will be more than a dozen people on the list, ever.
This is as far as I've got. The problem is, that the poster gets an email too and they shouldn't. Have no idea how to correct that.
Code:
## Variables to use
$mbttle = "Message Board";
$mbsubj = "New Message Posted";
$viewit = "http://thedomain.net";
$from = "me\@mine.net";
$userdb = "mail.txt";
$tester = "test.txt"; # temporary for tests
$poster = "Dan"; ## temporary for tests
## Mail message
@msg = "
A new message as been posted by:$poster
View: $viewit\
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-";
open (UDB,"$userdb") or die "can't open $userdb: $!";
@ud=<UDB>; close (UDB);
foreach $usr (@ud) {
($nm,$em)=split(/\|/,$usr);
print qq ~$nm, $em<br>~; ## just to see the list
## write to test file like it is sendmail
### -- Doesn't do it right. Posters name should not be there
open (TSF,">>$tester") or die "can't open $tester: $!"; @tf=<TSF>;
print TSF "To: $em\n";
print TSF "From: $from\n";
print TSF "Subject: $mbsubj\n";
print TSF "@msg\n";
close (TSF);
} # end foreach
Can anyone help on this please?
Thanks.
11-21-2012, 02:09 AM
Sixtease
Here's how I do it:
Code:
use Email::MIME;
use Email::Sender::Simple;
use Email::Sender::Transport::SMTP;
sub _sendmail {
my ($mail, $cfg) = @_;
my $to = $mail->{to};
my $from = $mail->{from};
my $subject = $mail->{subject};
my $body = $mail->{body};
my $email = Email::MIME->create(
header => [
From => $from,
To => $to,
Subject => $subject,
Bcc => 'an_address_where_I_want_a_copy@example.com',
'Content-Type' => 'text/plain;charset=UTF-8',
],
parts => [
Email::MIME->create(
attributes => {
content_type => 'text/html',
charset => 'UTF-8',
},
body => $body,
),
],
);
my $transport = Email::Sender::Transport::SMTP->new({
host => $cfg->{host},
sasl_username => $cfg->{account},
sasl_password => $cfg->{password},
});