www.webdeveloper.com
Results 1 to 7 of 7

Thread: Mail send problem

  1. #1
    Join Date
    Jun 2008
    Posts
    223

    Mail send problem

    Hi, on a little blog script I've put together, I can get a list of commenters to notify of a new comment. That's fine and the poster is not included. The trouble I'm having is, sending the mail out from that list.

    The List
    Code:
    one@some.com|blog@mine.com|The subject|The message|
    two@where.com|blog@mine.com|The subject|The message|
    Code:
    open(MMK, "<$mailtemp"); @mmsg = <MMK>; close(MMK);
    
     foreach $ml (@mmsg) {
     chomp($ml);
     @nt=split /\|/, "$ml";
    
     open (SENDMAIL, "|$smpath -t") or die "cannot open sendmail: $!";
     print SENDMAIL "To: $nt[0]\n";
     print SENDMAIL "From: $nt[1]\n";
     print SENDMAIL "Subject: $nt[2]\n";
     print SENDMAIL "$nt[3]\n\n";
     close (SENDMAIL);
     }
    When I try this all I get is one email being sent.

    What do I have to do run all the emails in the list?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    389
    Use a module. This is a function from my app that sends mail to users:

    Code:
    use Email::MIME;
    use Email::Sender::Simple;
    use Email::Sender::Transport::SMTP;
    
    sub _sendmail {
        my ($c, $mail) = @_;
    
        my $to = $mail->{to};
        my $from = $mail->{from} || 'news@example.org';
        my $subject = $mail->{subject};
        my $body = $mail->{body};
        my $email = Email::MIME->create(
            header => [
                From => $from,
                To => $to,
                Subject => $subject,
                Bcc => 'mail-log@example.org',
                'Content-Type' => 'text/plain;charset=UTF-8',
            ],
            parts => [
                Email::MIME->create(
                    attributes => {
                        content_type => 'text/html',
                        charset => 'UTF-8',
                    },
                    body => $body,
                ),
            ],
        );
        my $cfg = { ... };
        my $transport = Email::Sender::Transport::SMTP->new({
            host          => $cfg->{host},
            sasl_username => $cfg->{account},
            sasl_password => $cfg->{password},
        });
    
        print {$_STDERR} "Sending mail to $to\n";
    
        undef $@;
        eval { Email::Sender::Simple->send($email, {transport => $transport}) };
        if ($@) { warn "Sending email failed: $@" }
    }
    used as

    Code:
    _sendmail($c, {
        to => 'user@example.com',
        from => 'foo@example.org',
        subject => 'news from foo inc.'
        body => $mail_content,
    });

  3. #3
    Join Date
    Jun 2008
    Posts
    223
    That looks good that does, but unfortunately those modules are not on the cheap server that I've used for hosting the next year for my treks in Spain (and I'm the only Perl user on the whole thing - they had to turn Perl on specially for me).

  4. #4
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    389
    Why don't you install them in your storage yourself?

  5. #5
    Join Date
    Jun 2008
    Posts
    223
    I'll have to see if they'll let me. Only had server a few days and just testing stuff out before September. A previous host on a short term site wouldn't let me do that.

    Figured you'd be on holiday this time of year.

  6. #6
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    389
    I don't get it. What do you mean if they let you? It's a hosting where you have some space and you can upload whatever source code you want, no?

  7. #7
    Join Date
    Jun 2008
    Posts
    223
    On the higher end hosts that have been going for 10 years or so, yes, a lot do. Others have a thing in Cpanel where you can install a module (but some hosts turn that off). I think the newer ones, where nobody uses Perl, might be a bit scared of it. This is just a dirt cheap host and while I get good response from them, they're geared mainly to PHP.

    Let's face it, about 1 percent of websites now use Perl and of that 75 percent use 5.8 or earlier. We're a dying breed we are.

    Noticed a number of new hosts, don't even have Perl or Python at all. They're just pure PHP.

    Anyways, Solved it. Used for, instead of foreach. Local tests only showed the last one - live test gave me 4 of each.

    Changing the foreach loop to for gave me the correct emails and the temp file is now unlinked, wait for the next time a comment is made. Will have to give the temp file a unique name each time to prevent mess ups.

    I so rarely use a for loop, that I forgot about it. Duh.

    Hey Sixtease, I'm getting old (68 in a couple of months). New kids on the block do newer (but not always safer) things.

    The mini blog script is only 50k and will do what I want for a short term thing.

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