This topic is locked
[SOLVED]

Best way to configure smtp settings to send emails in phprunner 10.7

5/19/2022 6:48:22 AM
PHPRunner General questions
A
alfonso authorDevClub member

In version 10.7 I see several ways to configure the smtp service to send emails. One of them is Use PHP mail function(notrecomended) and the other one is Use mail settings set in thephp.ini file but I don't know how it is actually done.
I have also seen in a ticket that it can be configured in After application initialized with these parameters but don't run to me:
$globalSettings["strSMTPUser"] = "";

$globalSettings["strSMTPServer"] = "localhost";

$globalSettings["strSMTPPort"] = "25";

$globalSettings["strSMTPPassword"] = "";

$globalSettings["strFromEmail"] = "";

Any idea? Thanks

Admin 5/19/2022

This is something that you cannot guess and your SMTP settings depend on what SMTP provider do you use be that Gmail, Office 365 etc. All email providers explain how to configure their SMTP settings and this is what you need to follow.

A
alfonso authorDevClub member 5/19/2022

I've got it with smtp settings in Misc->Email settings. All is ok.

Now I would like to use an email template with html instead of building the email content line by line with this:
$message = '<html><body>';
$message .= '<img src="popo.jpg" />';
$message .= '<table style="border:1px dotted #ccc" cellspacing=4" cellpadding="10" width=100%>';
$message .= "<tr><td style='background: #eee;'>NAME: </td><td>" . $values["listing_name"] . "</td></tr>";
...

aadham 5/20/2022

Hi Alfonso

The best approach, for me at least, was to create a table for the mail settings, so that it can be easily modified through the app and can help using more than one mail server, if need be. The table can look like this:

img alt

I've also created another table that contains HTML templates for the different emails my app would typically send. This table can look like this:

img alt

Using a few lines of code in "After Record Added/Updated" event, you can send emails easily to whomever you wish.

I hope you'll find the above useful.

D
DealerModulesDevClub member 5/20/2022

Hi aadham and thanks for your share.

This is probably the best way I have seen so far to handle emails using PHPRunner.

Can you share more specifics about the "Using a few lines of code in "After Record Added/Updated" event"?

Do you have a code example for the events?

Thank you so much for sharing.

Paul

A
alfonso authorDevClub member 5/20/2022

Thank you so much for the idea. I agree that an example would be ideal to develop your idea

aadham 5/21/2022

In our case, a project coordinator can choose to send a notification email through the app once an employee has been assigned a task. Naturally, this needs data from the Employees, Tasks, Task Types and Task Rates tables:
A checkbox (notify_employee) can be used to either send the notification email or just assign the task without sending the email.
The following code can be used in the "After record added/updates" events, as follows:
This part collects the required info:

if($values['notify_employee'] == "1")
{
$sql = "SELECT
tasks_qc.email_qc,
employees.emp_firstname,
tasks_qc.task_deadline_qc,
tasks_qc.script_avail,
task_types.Task_Type
FROM tasks_qc
INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id
INNER JOIN task_rates ON tasks_qc.emp_id_qc = task_rates.emp_id
INNER JOIN task_types ON tasks_qc.task_type_ID_qc = task_rates.task_type_ID
WHERE tasks_qc.order_det_id =".$values[order_det_id];
CustomQuery($sql);
$email3=DBLookup($sql);
$firstname=DBLookup("select employees.emp_firstname FROM tasks_qc INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$tasktype=DBLookup("SELECT task_types.Task_Type FROM task_types INNER JOIN tasks_qc ON tasks_qc.task_type_id_qc = task_types.task_type_ID WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$html=DBLookup("SELECT body from task_email where task_email_id=1");
$full_title=DBLookup("SELECT full_title FROM tasks_qc INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$media_ID=DBLookup("SELECT media_ID FROM tasks_qc INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$deadline=DBLookup("SELECT task_deadline_qc FROM tasks_qc INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$script=DBLookup("SELECT if(`script` = 1, 'Attached', 'Unavailable') AS `script` FROM tasks_qc INNER JOIN employees ON tasks_qc.emp_id_qc = employees.emp_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$admincomment=DBLookup("SELECT admin_comment FROM tasks_qc INNER JOIN order_details ON order_details.order_det_id = tasks_qc.order_det_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$taskfrom=DBLookup("SELECT task_from FROM tasks_qc INNER JOIN order_details ON order_details.order_det_id = tasks_qc.order_det_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$taskto=DBLookup("SELECT task_to FROM tasks_qc INNER JOIN order_details ON order_details.order_det_id = tasks_qc.order_det_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);
$media_avail=DBLookup("SELECT tasks_qc.media_avail_id FROM tasks_qc INNER JOIN order_details ON order_details.media_avail_id = tasks_qc.media_avail_id WHERE tasks_qc.order_det_id =".$values[order_det_id]);

The following handles the email message itself:

$email=$values["email_qc"];
$email2="johndoe@whatever.com";
$sendto=$email . "," . $email2.",".$email3;
$from="johndoe@whatever.com";
$msg="";
$subject= "New " .$tasktype. " Task - (" .$full_title. ")";
$fileArray = my_json_decode($values["script_avail"]);
$attachments = array();
$msg = "";
foreach($fileArray as $f)
{
$attachments[] = array("path" => $f["name"]);
}
$msg = $html;
$msg = str_replace("##firstname##",$firstname,$msg);
$msg = str_replace("##tasktype##",$tasktype,$msg);
$msg = str_replace("##full_title##",$full_title,$msg);
$msg = str_replace("##media_ID##",$media_ID,$msg);
$msg = str_replace("##deadline##",$deadline,$msg);
$msg = str_replace("##script##",$script,$msg);
$msg = str_replace("##admin_comment##",$admincomment,$msg);
$msg = str_replace("##task_from##",$taskfrom,$msg);
$msg = str_replace("##task_to##",$taskto,$msg);
$msg = str_replace("##media_avail##",$media_avail,$msg);
$ret=runner_mail(array('to' => $sendto, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from, 'charset' => 'UTF-8', "attachments" => $attachments));

This is the Tasks Emails table which contains the different email templates:

img alt

The above code is nearly three years old now and being a non-developer, I've found PHPRunner's help files and examples as well as this forum's Tips and Tricks quite valuable and helpful when I wrote/cusomized the above to serve our needs.
I'm quite sure there might be room for improvement or optimization, but that worked and still works for us, flawlessly.
I'll be pleased to answer any questions regarding the above code to the best of my modest ability as soon as I can.

A
alfonso authorDevClub member 5/21/2022

I like it. Thanks

U
ustunsoz 8/24/2022

Dear aadham,

could you please elaborate how to use database smtp settings for multiple server settings in each sending instance.

I believe your code sample shows only how to use template, not overriding the system mail configuration

Thanks in advance...


Please Ignore my post, I just found Sergey already answered in http://asprunner.com/forums/topic/25265-smtp-settings-in-database-mysql/

Thanks