Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

cant send a mass email

by Anonymous Monk
on Mar 19, 2003 at 00:05 UTC ( [id://244193]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie so please be patient... I am trying to send a mass email, but have been unsuccessful. The problem is with the way I have my variable $email setup. If I have $sender->to('myemail@somewhere.com' , 'hisemail@somewhere.com') it works. The Email.txt file is just a list of emails, one on each line. Thanks for any help
open(EMAIL, "email.txt"); while(<EMAIL>){ chomp; my $val= $_; push(@email, "'$val'");} close(EMAIL); my $email = join ",", @email; my %mail; open(MSG, "message.txt"); while (<MSG>){ if ($_ =~ /^subject:(.*)/){ $mail{'subject'} = $1;} else{ $mail{'msg'} .= $_;}} my $sender = Net::SMTP->new ('mailhost', Debug=> 1); $sender->mail('anywhere@home.com'); $sender->to($email); $sender->data(); $sender->datasend ("From: Home\n"); $sender->datasend ("Subject: $mail{'subject'}\n\n"); $sender->datasend ("\n"); $sender->datasend ("$mail{'msg'}"); $sender->dataend(); $sender->quit();

Replies are listed 'Best First'.
Re: cant send a mass email
by cees (Curate) on Mar 19, 2003 at 02:52 UTC

    Try something like the following (untested) code:

    use Mail::Bulkmail; my ($subject, $message); open(MSG, "message.txt"); while (<MSG>) { if ($_ =~ /^[Ss]ubject:\s*(\S*)/) { $subject = $1; } else { $message .= $_; } } close MSG; my $bulk = new Mail::Bulkmail( Message => $message, Subject => $subject, From => 'Home', LIST => 'email.txt' use_envelope => 1, ); $bulk->bulkmail();

    If you have an older version of Mail::Bulkmail than you can simplify this even more by using the HFM (Headers From Message) feature that has dissapeared from version 3.x.

    use Mail::Bulkmail; open(MSG, "message.txt"); undef $/; my $message = <MSG>; close MSG; my $bulk = new Mail::Bulkmail( Message => $message, From => 'Home', LIST => 'email.txt' use_envelope => 1, HFM => 1, # pull headers from message );
Re: cant send a mass email
by diotalevi (Canon) on Mar 19, 2003 at 00:12 UTC

    Do us a favor and explain what you plan to do with this. On its face it looks like you're asking for help with e-mail marketi spam.

Re: cant send a mass email
by ibanix (Hermit) on Mar 19, 2003 at 01:20 UTC
    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;
      I have to send a mass email to a group of people in my company. I have tested it out by sending it to my own email account and it worked. Is there a way to setup a variable with everyones email that will allow me to go through the scrip once rather than use a while loop? Thanks again for the help
        Is there a way to setup a variable with everyones email that will allow me to go through the scrip once rather than use a while loop?

        Why does it matter? As I already said, your email server may refuse to send emails with an excessive number of recpienents. You are better left to sending one email per address.

        ibanix

        $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: cant send a mass email
by The Mad Hatter (Priest) on Mar 19, 2003 at 01:47 UTC
    I don't know anything about Net::SMTP, but might the problem be the single quotes you put around the address when you do push(@email, "'$val'");? Try using push(@email, $val); instead and see if it works.

    Update: Silly me, didn't realize the problem. Anyway, you could probably use an eval to solve the problem, but as others said, a loop would be easiest (and most reliable).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://244193]
Approved by ibanix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found