[英]PHP mass sending email script
我正在尝试在小型本地分类网站中为我们的用户制作电子邮件群发发送器。
想法是:我使用upload.html上传带有电子邮件列表的emails.txt文件,每行分开
当upload.html 表单被doskaosender.php 处理后,它读取该文件并将其放置在本地目录中以供下一步处理。 然后我去send.html,输入主题、发件人、回复和消息并点击发送,它被send.php处理。 在send.php 中,我创建了doskaosendmail 函数,它接收主题、发件人、回复和消息并发送单个电子邮件。
然后我只是循环读取email.txt,逐行读取每一行并将eamil 传递给单个发件人doskaosendmail 函数。
但是我在 send.php 中遇到错误,出了点问题,我无法确定到底是什么。
编码。
上传.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 后得到了错误“发送过程错误”(脚本的最后一个分支)。
请帮我看看有什么问题?
你的函数没有返回任何东西,这就是你出错的原因,请将此函数修复到这个 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.