www.webdeveloper.com
Results 1 to 5 of 5

Thread: A simple mailer

  1. #1
    Join Date
    Jun 2008
    Posts
    223

    A simple mailer

    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.

    File - mail.txt
    Code:
    Dan|dan@dada.com|
    Sam|sam@sasa.net|
    Bob|bob@bobo.org|
    Joe|joe@jojo.com|
    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.

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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},
        });
    
        _log "Sending mail to $to\n";
    
        undef $@;
        eval { Email::Sender::Simple->send($email, {transport => $transport}) };
        if ($@) { _log "Sending email failed: $@" }
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    223
    Hi Sixtease, my host doesn't have those modules installed.

    At the moment I'm still building it on my localhost, so using the file.

    Once done I'll just use sendmail as usual

    I did try an if next, but that didn't work.

  4. #4
    Join Date
    Jun 2008
    Posts
    223
    I think I've done it. It hasn't broken so far
    Code:
    foreach $usr (@ud) {
     ($nm,$em)=split(/\|/,$usr);
       if($usr =~ m/\b$poster\b/) {  next; }
     print qq ~$nm, $em<br>~; ## just to see the list
    I tried a different way of next and it seems to do the trick

  5. #5
    Join Date
    Jun 2008
    Posts
    223
    Oops. didn't mean to start this reply

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles