Page 1 of 1

Function sending email

Posted: Sat Apr 06, 2019 10:27 pm
by christ2000

Hello guys, i am using this funtion on a custom php file to sent email to user but nothing happens, could someone check if code it is ok? Thanks

<?php
function Email_Sending($email, &$args) {
//var_dump($email, $args);
//exit();

	$email->Recipient = $args["rsnew"]["Email"]; // Change recipient to a field value in the new record
	$email->Subject = "My New Subject"; // Change subject
	$email->Content .= "\nAdded by " . CurrentUserName(); // Append additional content

return TRUE;

}
?>


Re: Function sending email

Posted: Sun Apr 07, 2019 10:42 pm
by mobhar

The "Email_Sending" is a server event, and should be used only for the related built-in table that generated by PHPMaker. In other words, you cannot use it for your Custom File.

However, you may simply use "SendEmail" global function (see this code in more detail from the generated "phpfn15.php" -- assume you're using v2019), and make sure you have already enabled "Include common files" for your Custom File in order to use that global function.

Just follow the param in that function, for example:

SendEmail("abc@something.com", "cde@something.com", "", "", "This Subject is for Test", "This is the content of email, blablabla...", "html", "utf-8", "");

Just try to play with it, then you'll know what you should do next.


Re: Function sending email

Posted: Mon Apr 08, 2019 10:08 am
by christ2000

my custom file code is:

<?php
$dbhelper = &DbHelper();
?>

<?php
$sql = "SELECT * FROM ExpiredDash";
echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash")));
?>

this show me now a table with users with expired documents ( Name, document expired and email) , so how add this sendemail function to this code to send email to this users to advice they have expired documents?

thanks in advance


Re: Function sending email

Posted: Mon Apr 08, 2019 10:12 am
by mobhar

Since ExecuteHtml will return the output your content in html table, then simply assign this output to $mailContent param.

In other words, from the previous example I gave above, just change this:
"This is the content of email, blablabla..."

to:
$dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash")))

or if it does not work either, then try this:

$mailContent = $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash")));
SendEmail("abc@something.com", "cde@something.com", "", "", "This Subject is for Test", $mailContent, "html", "utf-8", "");


Re: Function sending email

Posted: Mon Apr 08, 2019 11:30 am
by christ2000

OK... i try and now send the email but how can a insert the employee email that appear on the table as toEmail ? also the problem is i need system just send the expired documents to each employee and not all documents to all employees. is this possible?

i change the code to get mailto, but email do no is sent

<?php
$dbhelper = &DbHelper();
?>

<?php
$sql = "SELECT * FROM ExpiredDash";
echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash")));
$emailto = ExecuteScalar("SELECT Email FROM ExpiredDash");
SendEmail("abc@something.com", $emailto, "", "", "This Subject is for Test", $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash"))), "html", "utf-8", "");
?>

any idea?


Re: Function sending email

Posted: Tue Apr 09, 2019 8:59 am
by mobhar

Have you already enabled "Include common files" option for your Custom File?


Re: Function sending email

Posted: Tue Apr 09, 2019 9:26 am
by christ2000

Hello, yes i included using checkmark on creating page

my header code on page:

<?php
include_once("conexao.php");
require_once('phpfn15.php');
require_once('ewcfg15.php');
?>

the full page code:

<?php
$dbhelper = &DbHelper();
?>

<?php

$sql = "SELECT * FROM expireddash";
echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("expireddash")));
$emailto = ExecuteScalar("SELECT Email FROM expiredDash");

SendEmail("abc@something.com", $emailto, "", "", "This Subject is for Test", $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("ExpiredDash"))), "html", "utf-8", "");

?>

any idea?


Re: Function sending email

Posted: Tue Apr 09, 2019 9:33 am
by mobhar

If you obviously have enabled "Include common files" option for your Custom File, no need to add this following code anymore:
<?php
include_once("conexao.php");
require_once('phpfn15.php');
require_once('ewcfg15.php');
?>

In addition, please refer to the "home.php" Custom File from the demo project. You can learn from it how to use Custom File, and how to expose some global functions in it. I've just tried this following code in "home.php" file and it works properly:
<?php
$emailto = ExecuteScalar("SELECT Email FROM employees");
echo "Email: " . $emailto;
?>


Re: Function sending email

Posted: Tue Apr 09, 2019 10:30 am
by christ2000

Thanks, now email send is working, the problem was in header.

the only need to fix is that just employee get the documents under their account, now employees received all documents so employed 1 received expired documents form employee 1 and employee 2 and vise versa .

is there any way to reach this point?

also note now that just first record is got so just first email on list, no all echo also show one email

<?php
$dbhelper = &DbHelper();
?>

<?php

$sql = "SELECT * FROM expiredbyemail";
echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("expiredbyemail")));
$emailto = ExecuteScalar("SELECT Email FROM expiredbyemail");
$rname = ExecuteScalar("SELECT Document Name FROM expiredbyemail");
echo "Email: " . $emailto;

SendEmail("ab@msn.com", "$emailto", "", "", "You have expired requirements", "$rname", "html", "utf-8", "");

?>
any idea hoe repeat for all records that appear? thanks


Re: Function sending email

Posted: Tue Apr 09, 2019 12:45 pm
by mobhar

You need to loop using Execute() global function, since ExecuteScalar() will only return the value from the first column and the first record that found. Inside the loop, then call the SendEmail function which will get the param from the related field of recordset that returned by the loop.

For your reference, please see this topic: http://www.hkvforums.com/viewtopic.php?f=4&t=44268

Always try it by yourself first, and post your code for more discussion.


Re: Function sending email

Posted: Wed Apr 10, 2019 10:12 am
by christ2000

i used this code but got error;

<?php
$dbhelper = &DbHelper();
//$sql = "SELECT * FROM expiredbyemail";
//echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("expiredbyemail")));
$rname = ExecuteScalar("SELECT Document Name FROM expiredbyemail");
$rsEmail = Execute("SELECT Email FROM expiredbyemail");
if ($rsEmail && $rsEmail->RecordCount() > 0) {
while (!$rsEmail->EOF) {
$emailto = $rsEmail->fields("Email");
if ($rsname && $rsname->RecordCount() > 0) {
while (!$rsname->EOF) {
$emailto = $rsEmail->fields("Email");
SendEmail("email@demo.com", "$emailto", "", "", "You have expired requirements", "$rname", "html", "utf-8", "");
$rsEmail->moveNext();
}
$rsEmail->close();
}
$rsname->moveNext();
}
$rsname->close();
} else {
echo "No Email from expiredbyemail table found";
}
?>


Re: Function sending email

Posted: Wed Apr 10, 2019 10:25 am
by mobhar

Please try this:

<?php
$dbhelper = &DbHelper();
//$sql = "SELECT * FROM expiredbyemail";
//echo $dbhelper->ExecuteHtml($sql, array("fieldcaption" => TRUE, "tablename" => array("expiredbyemail")));
$rsEmail = Execute("SELECT Email FROM expiredbyemail");
if ($rsEmail && $rsEmail->RecordCount() > 0) {
while (!$rsEmail->EOF) {
$emailto = $rsEmail->fields("Email");

$rsName = Execute("SELECT Document Name FROM expiredbyemail");
$rname = "";
if ($rsName && $rsName->RecordCount() > 0) {
while (!$rsName->EOF) {
$rname .= $rsName->fields("Document Name") . ",";
$rsName->moveNext();
}
$rname = rtrim($rname, ",");
$rsName->close();
} else {
$rname = "no documents from expiredbyemail are found";
}

// adjust someone@something.com to your desired email
SendEmail("someone@something.com", "$emailto", "", "", "You have expired requirements", "$rname", "html", "utf-8", "");

$rsEmail->moveNext();
}
$rsEmail->close();
} else {
echo "No Emails from expiredbyemail table are found";
}
?>


Re: Function sending email

Posted: Wed Apr 10, 2019 10:55 am
by christ2000

hello, system keep sending all docs to all employees and not just employees's docs that is a probem


Re: Function sending email

Posted: Wed Apr 10, 2019 11:01 am
by mobhar

You may add the WHERE criteria in this following SQL to filter the document name based on the email:

"SELECT Document Name FROM expiredbyemail"

to the following one:

"SELECT Document Name FROM expiredbyemail WHERE Email = '" . $emailto . "'"


Re: Function sending email

Posted: Wed Apr 10, 2019 11:09 am
by christ2000

Problem solve, thanks a lot for your help.


Re: Function sending email

Posted: Wed Apr 10, 2019 11:11 am
by mobhar

You're welcome, I am so glad to resolve the problem.