【发布时间】:2016-02-19 11:59:21
【问题描述】:
我正在编写一个程序,通过 ajax 向我们的注册用户发送批量电子邮件。
我想在每个循环响应完成后回显它并进入下一个条件。
例如:-
我在数据库中有 100 封电子邮件的列表。当我提交编程请求时,它将开始发送电子邮件。
程序。像这样工作:
<?php
foreach($emails as $email){
$status = $this->sendMail($email);
if($status == true)
{
echo "Mail Sent";
}else{
echo "Not Sent";
}
}
?>
现在我想为每个循环一次又一次地打印“邮件已发送”/“未发送”。
输出:-
邮件发送
邮件发送
邮件发送
未发送
邮件发送
发送中..
编辑
我的 PHP 代码是:-
<?php
public function send_message() {
$sendTo = $this->input->post('send_to');
$template = $this->input->post('template');
$subject = $this->input->post('subject');
switch ($sendTo) {
case 1:
$users = $this->getAllEmails();
break;
case 2:
$users = $this->getRegisteredUsersEmails();
break;
case 3:
$users = $this->getTemproryUsersEmails();
break;
case 4:
$users = $this->getSubscribersEmails();
break;
}
$status = $this->sendMail($users, $template, $subject);
echo "Mail Sent";
}
private function sendMail($users, $template, $subject) {
$this->load->library('parser');
$status = array();
$i = 0;
foreach ($users as $user) {
$message = $this->parser->parse('email_templates/' . $template, array('email' => $user->email, 'name' => ($user->name != '') ? "Hi " . $user->name : "Hello"), TRUE);
$response = $this->mail->send(config_item('sender_mail'), config_item('sender_name'), $user->email, $subject, $message);
$status[$i]['email'] = $user->email;
$status[$i]['status'] = ($response == 1) ? 1 : 0;
$i++;
}
return $status;
}
?>
我的 Ajax 代码:-
<script type="text/javascript">
$("#send_mail").submit(function(){
$.ajax{
url:"<?php echo base_url('promotion/send_message'); ?>",
type:"post",
data:$(this).serialize(),
success:function(data){
$("#status").html(data);
}
}
});
</script>
【问题讨论】:
-
那有什么问题?
-
当我通过 ajax 提交请求时,它会在向所有人发送电子邮件后返回完整的响应。不是每个循环。我想要每个循环运行时的响应。
-
当我向 porg 提交 ajax 时,您没有得到我所说的 @Anant Think。它将开始循环发送电子邮件,完成该过程后,它将立即在“成功:功能(数据)”中发送所有“邮件发送/不发送”响应。我希望在发送电子邮件 1 时显示它打印“已发送邮件”并转到下一个并发送下一个,然后一次一个地打印“已发送邮件”。
-
先创建一个 jquery 邮件 ID 数组然后:-
$.each(mailid, function() { $.ajax({....your code and in this code alert(response)})}); -
程序从数据库中获取所有Mail id。我只需要点击发送按钮.. 你的 jquery 适用于用户提供的电子邮件列表。
标签: javascript php ajax email