简体   繁体   中英

PHP mass sending email script

I'm trying to make email mass sender for our users in small local classfieds site.

Idea is: I use upload.html to upload emails.txt file with list of emails, separated on each line

After upload.html form gets processed by doskaosender.php, it reads the file and and places it in local dir to be processed in the next step. Then I go to send.html, enter the subject, from, replyTo and the message and click SEND, it get's processed by send.php. In send.php I create the function doskaosendmail wich takes subject, from, replyTo and message and sends single email.

Then I just loop the reading of email.txt, take each line by line and pass the eamil to single sender doskaosendmail function.

But I got an error in send.php, something goes wrong, and I can't put my finger on what exactly.

The code.

upload.html

    <!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Upload Emails</title>
</head>
<body>
<h2><p><b> UPLOAD emails.txt file </b></p></h2>

<form action="doskaosender.php" method="post" enctype="multipart/form-data">
    <input type="file" name="filename"><br>
    <input type="submit" value="Upload"><br>
</form>

</body>
</html>

it goes to doskaosender.php

<?php
   // check if file uploaded
   if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
   {
       // check if there's already such file
       if(file_exists('emails.txt')){
       chmod('emails.txt',0755); //Change the file permissions if allowed
       unlink('emails.txt'); // if there's, then remove the file
   }
       // if uploaded successfully we move the file
       // from temp dir to the final
       move_uploaded_file($_FILES["filename"]["tmp_name"], "/home/u210471985/public_html/_misc/doskaosender/".$_FILES["filename"]["name"]);
   } else {
       echo("Error of uploading the file");
   }

/* HANDLE  and READ FILE UPLOADED */
echo "The list of emails to be processed:  <br>";

$fh = fopen('emails.txt','r');
while ($line = fgets($fh)) {

    echo($line);
}
fclose($fh);
?>

then we go to send.html to enter the message to be massevily sent

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>SEND</title>
</head>
<body>
<!--// $to, $subject, $message, $from, $replyTo-->
<h3> SCRIPT DOSKAOSENDER V0.1 beta </h3>

<form action="send.php" method="post">
    <input type="text" name="subject" placeholder="subject"/> <br>
    <input type="text" name="from" placeholder="from whom (email)"/> <br>
    <input type="text" name="replyTo" placeholder="reply to"/> <br>
    <textarea name="message" placeholder="text of email">
    </textarea>   <br>
    <input type="submit" value="START SENDING"/> <br>
</form>
</body>
</html>

then it gets processed by send.php

<?php

echo "Let's start sending! <br>";

if( isset($_POST["subject"]) &&
    isset($_POST["from"]) &&
    isset($_POST["replyTo"]) &&
    isset($_POST["message"])
    )
 {
    echo "The form fullfilled correctly sending process has been started. ";
        // we open the file emails.txt to get the emails
     $fh = fopen('emails.txt','r');
     while ($toEmail = fgets($fh)) {

         $send = doskaosendmail($toEmail,$_POST["subject"],$_POST["message"],$_POST["from"], $_POST["replyTo"] );
         if($send){
             echo "Email has been sent to: " . $toEmail . "<br>";
         } else { echo "<b> FAILED TO SEND email to: " . $toEmail . "</b><br>";  }
     }
     fclose($fh);
}else{
    echo "Error of sending process";
}

function doskaosendmail($to, $subject, $message, $from, $replyTo)
{
    $headers = 'From: ' . $from . "\r\n" .
        'Reply-To: ' . $replyTo . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);

}

?>

so I got after processing send.php and error "Error of sending process" (last branch of script).

Please help me find what's wrong?

Your function was not return any thing this is why you were getting error please fix this function to this doskaosendmail

function doskaosendmail($to, $subject, $message, $from, $replyTo) {
        $headers = 'From: ' . $from . "\r\n" .
                'Reply-To: ' . $replyTo . "\r\n" .
                'X-Mailer: PHP/' . phpversion();

        if (@mail($to, $subject, $message, $headers)) {
            return true;
        } else {
            return false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM