You didn't describe what your problem was. What happens? What's the error? It will help if you add
use warnings;
use strict;
to the start of your code. This will enable warnings and force you to declare your variables, which greatly aids in catching mistakes.
Now, on the topic of your code: You're trying to send an email with one very long list of recipients. It's possible your email server will not honor this. You may want to try sending one email/address, like so (code UNTESTED):
use strict;
use warnings;
# Define constants
my $address_file = "email.txt";
my $message_text = "message.txt";
my $from_addr = "anywhere@home.com";
my $mailhost = "localhost";
# Always check to see if file opened or not
open(MSG, "$message_text") || die "Could not open $message_text! $!\n"
+;
my $subject;
my $body;
LINE: while(<MSG>) {
if /^subject:(.*)/ { $subject = $1 and next LINE};
$body .= $_;
}
close(MSG);
open(LIST, "$address_file") || die "Could not open $address_file! $!\n
+";
# Send an email for each address in the file
# Note we're not doing any validation on addresses
while(<LIST>) {
chomp;
my $sender = Net::SMTP->new("$localhost");
$sender->mail("$from_addr");
$sender->to("$_");
$sender->data();
$sender->datasend ("From: Home\n");
$sender->datasend ("Subject: $subject\n\n");
$sender->datasend ("$body\n");
$sender->dataend();
$sender->quit();
}
close(LIST);
Hope this helps. Cheers,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|