Hi Everyone!
In case I desire to send an auto email without user intervention to notify actions
to selected recipients, is it possible?
The "emailIt" JavaScript function requires a user to fill-up and manually trigger
the sending of email.
Any ideas, tips or suggestions are welcome!
Thanks very much,
Mateo
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Auto Email
-
- Posts: 100
- Joined: Mon Jun 15, 2009 10:04 am
Re: Auto Email
Here is some sample code to get you started:
//In-order for these functions to work you need to make sure your
//environment has the following conditions met:
// 1. You need make sure that your web-server as write permissions to this folder {system path}/productionnu2/temppdf/
// 2. The report that you need to call must have its 'Form' (that is part of the criteria screen) set to 'Access without login - Yes'
// 3. Cut n Paste this into a Procedure and edit the values below
// settings
$dir = "/db/nufinancial_module";
$server_url = "http://secure.nubuilder.com/productionnu2/"; //Notice no SSL for this url
// write your own code to loop through your database and get the values for the following variables
$report_name = "FM007"; //This is the report number or code
$file_name = "DebtorsLists";
$email = "target@emailaddress.com";
$subject = "TEST Auto Email";
//$extra_params = "&todate=$todate&fromdate=$fromdate&customer=$customer_id"; //example
$extra_params = ""; //example
//call
makeUrlSendEmail($dir,$server_url,$email,$subject,$report_name,$file_name,$extra_params);
function makeUrlSendEmail($dir,$server_url,$email,$subject,$report_name,$file_name,$extra_params) {
// Insert some sys variables so that we can run reports without logging in.
$report1_zzsvar_id = uniqid(1);
$report1_form_ses_id = uniqid(1);
nuRunQuery("INSERT INTO zzsys_variable (zzsys_variable_id,sva_id) VALUES ('$report1_zzsvar_id', '$report1_form_ses_id')");
// Make the URL of the report with our new form session IDs.
$fqurl1 = $server_url."runpdf.php?x=1&dir=".$dir."&ses=".$report1_zzsvar_id."&form_ses=".$report1_form_ses_id."&r=".$report_name.$extra_params;
//create file on server
$pfile1 = array();
$pfile1[0] = getReportFile($fqurl1, ".pdf");
$pfile1[1] = "$file_name.pdf";
//send email
sendAttachedPDFreport($email, array($pfile1),$subject);
//delete file
@unlink($pfile1[0]);
}
function sendAttachedPDFreport($to, $pfiles, $subject) {
require_once("phpmailer/class.phpmailer.php");
$from = "noreply@nubuilder.com";
$fromname = "YOUR DETAILS HERE";
$wordWrap = "50";
$content = "<br>This is an auto generated system email, if you need to speak to someone about this email contact PUT YOUR OWN DETAILS HERE!.<br>";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->Host = "127.0.0.1";
$mail->FromName = $fromname;
$mail->AddAddress($to);
$mail->From = $from;
$mail->WordWrap = $wordWrap;
$mail->IsHTML("HTML");
$echolist = "";
for ($i = 1; $i <= count($pfiles); $i++) {
$mail->AddAttachment($pfiles[$i-1][0],$pfiles[$i-1][1]);
$echolist .= $pfiles[$i-1][1]."<br>\n";
}
$mail->Subject = $subject;
$mail->Body = $content;
$success = $mail->Send();
if ($success) {
echo "Successfully sent email to: $to<br>\n";
echo $echolist."<br>\n";
} else {
echo "Failed to send email to: $to<br>\n";
echo $echolist."<br>\n";
}
return $success;
}
function getReportFile($fqurl, $ext) {
$emailID = uniqid();
$pfile = dirname(__FILE__)."/temppdf/".$emailID.date("YmdHis").$ext;
$out = fopen($pfile, 'wb');
$in = fopen($fqurl, 'r');
while (!feof($in))
fwrite($out, fread($in, 8192));
fclose($in);
fclose($out);
return $pfile;
}//end function
//In-order for these functions to work you need to make sure your
//environment has the following conditions met:
// 1. You need make sure that your web-server as write permissions to this folder {system path}/productionnu2/temppdf/
// 2. The report that you need to call must have its 'Form' (that is part of the criteria screen) set to 'Access without login - Yes'
// 3. Cut n Paste this into a Procedure and edit the values below
// settings
$dir = "/db/nufinancial_module";
$server_url = "http://secure.nubuilder.com/productionnu2/"; //Notice no SSL for this url
// write your own code to loop through your database and get the values for the following variables
$report_name = "FM007"; //This is the report number or code
$file_name = "DebtorsLists";
$email = "target@emailaddress.com";
$subject = "TEST Auto Email";
//$extra_params = "&todate=$todate&fromdate=$fromdate&customer=$customer_id"; //example
$extra_params = ""; //example
//call
makeUrlSendEmail($dir,$server_url,$email,$subject,$report_name,$file_name,$extra_params);
function makeUrlSendEmail($dir,$server_url,$email,$subject,$report_name,$file_name,$extra_params) {
// Insert some sys variables so that we can run reports without logging in.
$report1_zzsvar_id = uniqid(1);
$report1_form_ses_id = uniqid(1);
nuRunQuery("INSERT INTO zzsys_variable (zzsys_variable_id,sva_id) VALUES ('$report1_zzsvar_id', '$report1_form_ses_id')");
// Make the URL of the report with our new form session IDs.
$fqurl1 = $server_url."runpdf.php?x=1&dir=".$dir."&ses=".$report1_zzsvar_id."&form_ses=".$report1_form_ses_id."&r=".$report_name.$extra_params;
//create file on server
$pfile1 = array();
$pfile1[0] = getReportFile($fqurl1, ".pdf");
$pfile1[1] = "$file_name.pdf";
//send email
sendAttachedPDFreport($email, array($pfile1),$subject);
//delete file
@unlink($pfile1[0]);
}
function sendAttachedPDFreport($to, $pfiles, $subject) {
require_once("phpmailer/class.phpmailer.php");
$from = "noreply@nubuilder.com";
$fromname = "YOUR DETAILS HERE";
$wordWrap = "50";
$content = "<br>This is an auto generated system email, if you need to speak to someone about this email contact PUT YOUR OWN DETAILS HERE!.<br>";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->Host = "127.0.0.1";
$mail->FromName = $fromname;
$mail->AddAddress($to);
$mail->From = $from;
$mail->WordWrap = $wordWrap;
$mail->IsHTML("HTML");
$echolist = "";
for ($i = 1; $i <= count($pfiles); $i++) {
$mail->AddAttachment($pfiles[$i-1][0],$pfiles[$i-1][1]);
$echolist .= $pfiles[$i-1][1]."<br>\n";
}
$mail->Subject = $subject;
$mail->Body = $content;
$success = $mail->Send();
if ($success) {
echo "Successfully sent email to: $to<br>\n";
echo $echolist."<br>\n";
} else {
echo "Failed to send email to: $to<br>\n";
echo $echolist."<br>\n";
}
return $success;
}
function getReportFile($fqurl, $ext) {
$emailID = uniqid();
$pfile = dirname(__FILE__)."/temppdf/".$emailID.date("YmdHis").$ext;
$out = fopen($pfile, 'wb');
$in = fopen($fqurl, 'r');
while (!feof($in))
fwrite($out, fread($in, 8192));
fclose($in);
fclose($out);
return $pfile;
}//end function