PHP群发电子邮件脚本

时间:2015-06-15 08:00:35

标签: php email

我正在尝试为小型本地分类网站中的用户制作电子邮件群发发件人。

想法是:我使用upload.html上传带有电子邮件列表的emails.txt文件,每行分开

在doskaosender.php处理upload.html表单后,它会读取文件并将其放在本地目录中,以便在下一步中处理。 然后我去send.html,输入主题,来自,回复和消息,然后点击发送,它由send.php处理。在send.php中,我创建函数doskaosendmail,它接收主题,来自,replyTo和消息,并发送单个电子邮件。

然后我循环阅读email.txt,逐行逐行并将eamil传递给单个发送者doskaosendmail函数。

但是我在send.php中出错了,出了点问题,我无法确切地指出是什么。

代码。

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>

它转到 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);
?>

然后我们转到 send.html 输入要发送的消息

<!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>

然后由 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);

}

?>

所以我在处理send.php之后得到了错误&#34;发送过程出错&#34; (脚本的最后一个分支)。

请帮我找错?

1 个答案:

答案 0 :(得分:0)

你的功能没有返回任何东西这就是你收到错误的原因请将此功能修复到这个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;
        }
    }