我在客户端有一个注册页面,它接受图像和用户详细信息并向服务器发送POST请求。
在服务器中,我上传图片并将网址和其他详细信息插入用户表。完成后,我想通过欢迎消息向用户发送电子邮件。但是,上传图像和发送电子邮件会导致客户端的等待时间增加。这是我的功能:
public function register($name,$email,$password,$about){
$query = "SELECT * FROM user WHERE email= ? LIMIT 1";
$value = array($email);
$data = $this->_dalObj->sqlQuery($query,$value);
if(isset($data['data'][0]['email']) == $email){
return array("status" => 0, "message" => "Email Address Already Registered With Us!", "data" => "");
}
$password = base64_encode($password);
$password = md5($password);
$access_token = md5($email.$password.$email.time());
$target = "uploads/";
$user_image = $this->_funcObj->uploadImage($target, 'image');
$user_image = $user_image['image'];
$query = "INSERT INTO user (name,email,password,access_token,image,about) VALUES (?,?,?,?,?,?)";
$value = array($name,$email,$password,$access_token,$user_image,$about);
$data = $this->_dalObj->sqlQuery($query,$value);
$mail = $this->_funcObj->send_email($email,"Registration","You have Successfully Registered with us!");
return array("status" => 1, "message" => "Registration Successful!", "data" => $mail);
}
我想要做的是上传图片并将详细信息插入数据库,然后将响应发送到客户端Registration Successful
。发送响应后,应调用电子邮件功能,以减少前端的等待时间。有一个使用玉米工作的解决方案,但是还有其他解决方案来解决这类问题。
P.S:我正在使用PHPMailer
发送电子邮件
答案 0 :(得分:0)
恕我直言,如果你不想使用cron作业,并且你正在使用jQuery或类似的东西,你可以使用返回数组作为数据来被js读取,以便ajax调用另一个服务器脚本来发送它电子邮件。 也许,在你的地方,我会做这样的事情:
这是一个非常简单的例子
* upload_image.php *
<?php
/* here you stuff for the upload*/
echo json_encode(array('is_error' => false, 'userID' => 5, 'sendemail' => true));
* send_email.php *
<?php
$mailer = new PHPMailer();
/* do your mailer stuffs here */
try {
$sql['sql'] = "UPDATE table SET email_sent = ?";
$sql['params'] = array('i',1);
$sql['stmt'] = $YOURCLASSTOMANAGESQLQUERIES->query($sql['sql'],$sql['params']);
echo json_encode(array('is_error' => false, 'email_sent' => true));
} catch(Exception $ex)
{
echo json_encode(array('is_error' => true, 'email_sent' => false, 'error_message' => $ex->getMessage()));
}
*您的js文件*
var OBJ = {
upload : function(){
var file_input = $('input[name="fileUpload"]'),
file_data = file_input.prop('files')[0],
form_data = new FormData();
form_data.append('fileUpload', file_data);
jQuery.ajax({
url: 'upload_image.php',
cache: false,
async: true,
data : form_data,
type: 'post',
dataType: 'json'
}).done(function (d) {
if (d.is_error === true) {
// do something with you errors
}
// show something to the user, then call:
OBJ.sendemail(d);
}).fail(function (j) {
// do something with your j.responseText
});
},
sendemail : function(data){
jQuery.ajax({
url: 'send_email.php',
cache: false,
async: true,
data : data,
type: 'post',
dataType: 'json'
}).done(function (d) {
if (d.is_error === true) {
// do something with you errors
}
// do something
}).fail(function (j) {
// do something with your j.responseText
});
},
init : function(){
$(document)
.off('upload','sendemail')
.on('click.upload','button[data-upload="true"]', function(e){
e.preventDefault();
e.stopImmediatePropagation();
OBJ.upload();
});
}
}
jQuery(document).ready(function(){
OBJ.init();
});
答案 1 :(得分:-1)
我会建议你使用cron job,因为它更简单,更干净的解决方案和用户调用不应该依赖于emial库..