Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having problem with my send mail script. Mostly the problem is all about the progress bar. I want when I click submit that the progress bar starts moving and ends when the sending done.

Well, I have tried to take similar examples CGI::ProgressBar but I can't make it work.

#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI;
use MIME::Lite;
use CGI::ProgressBar qw/:standard/;

$| = 1; # Do not buffer output

my $CGI = CGI->new();

my $from_email = $CGI->param("from_email");
my $from_name = $CGI->param("from_name");
my $subject = $CGI->param("subject");
my $receipts = $CGI->param("receipts");
my $message  = $CGI->param("message");

# Newline format. send message to each email listed
my $output = join "
", split " ", $receipts;  

if ($from_email, $from_name, $subject, $receipts, $message) {
my $msg = MIME::Lite->new(
        # From Name: John Deo And Sent From Email: johndeo@mail.com
        From     => "$from_name <$from_email>",
        To       => $output,
        Subject  => $subject,
        Data     => $message

);
# Support HTML Message
$msg->attr("content-type" => "text/html");  
$msg->send;

if ($msg) {   # if mail starts sending
print progress_bar( -from=>1, -to=>100 );
for (1..100){
        print update_progress_bar;
        # Print ProgressBar
        $progressbar_html = qq{<div class="box">
    <div id="progressbar">
        <div></div>
    </div>
    <div class="text">Sending... 1%</div>
</div>
};
        sleep 1;

}
print $response = "Message Sent Successfully";
exit;
}

if(!$msg) {
print $response = "Message Failed To Send";
}
}

print "Content-type: text/html

";
print <<START_HTML;

<!DOCTYPE html>
<html>
<head>
<title>Simple Mailer</title>
<style type="text/css">
.box {
    border-radius: 10px;
    padding: 25px;
    background-color: rgba(51, 51, 51, 0.96);
    text-align: center;
}
#progressbar {
    border: 3px solid #fff;
    border-radius: 20px;
    padding: 2px;
}
#progressbar > div {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background-color: #fff;
    width: 1%;
    height: 18px;
    border: 1px solid rgba(0, 0, 0, 0);
    border-radius: 20px;
}
.text {
    color: #fff;
    margin-top: 15px;
    font-size: 21px;
    font-weight: bold;
}
</style>
</head>

<body>
<form method="post">
<label>From Email
<input type="text" name="from_email" />
<br />
<br />
From Name
<input type="text" name="from_name" />
</label>
<p>
  <label>Subject
  <input type="text" name="subject" />
  </label>
</p>
<p>
  <label>Receipts
  <textarea name="receipts"></textarea>
  </label>
</p>
<p>
  <label>message
  <textarea name="message"></textarea>
  </label>
</p>
<p>
  <label>Send
  <input type="submit" value="Submit" />
  </label>
</p>
</form>
<p>&nbsp;</p>

$progressbar_html
<p>$response</p>
</body>
</html>

START_HTML
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
259 views
Welcome To Ask or Share your Answers For Others

1 Answer

Firstly, I wouldn't suggest using a module which looks like it was abandoned over eight years ago.

Secondly, I'm really not sure how you think this is going to work. In pseudo-code, your program looks like this:

  • Set up CGI object
  • Extract mail parameters
  • Create mail
  • Send mail
  • Show a progress bar which updates every second for a hundred seconds

The main problem is the fact that the line that says $msg->send is where the mail is sent. Once that line has been executed, the mail has gone. There's no point in creating and updating a progress bar after what you're showing the progress of has completed.

You need to do something more like this:

  • Set up CGI object
  • Extract mail parameters
  • Create mail
  • Set up and display progress bar
  • Send mail (including regular call-back to update the progress bar)

Of course, it's that last part that is tricky. I haven't looked at MIME::Lite for years (it's mostly deprecated these days) but skimming the documentation, I can't see any way to make asynchronous callbacks from within the mail sending code.

One approach would be to create an email for every person in the recipients list. That way you could send them one at a time and update the progress bar after sending each one. But that's not a particularly efficient way to email multiple people.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...