Use sendmail

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Use sendmail

Post by bruceholt » 2020-11-28 04:23

I have the following in the tasks hooks file:

Code: Select all

function tasks_after_insert($data, $memberInfo, &$args){
		/* send an email notification when a new task is added */
		ob_start(); ?>
<h3>Hello <?php echo sqlValue("select name from employees where id='" . makeSafe($data['assigned_member']) . "'"); ?></h3>		
<h3>You have been assigned a new task. Here are the details.</h3>
<hr>
<table>
	<tr><td><b>Task: </b></td><td><?php echo $data['task']; ?></td></tr>
	<tr><td><b>Task date: </b></td><td><?php echo date('d/m/Y', strtotime($data['date'])); ?></td></tr>
	<tr><td><b>Task details: </b></td><td><?php echo $data['task_details']; ?></td></tr>
	<tr><td><b>Assigned by: </b></td><td><?php echo $data['created_by']; ?></td></tr>
	<tr><td><b>Assigned to: </b></td><td><?php echo sqlValue("select name from employees where id='" . makeSafe($data['assigned_member']) . "'"); ?></td></tr>
	<tr><td><b>NOTE: DO NOT REPLY TO THIS EMAIL.</b></td></tr>
</table>		
		<?php
		$mail_body = ob_get_contents();
		ob_end_clean();
		
		$email = sqlValue("select email from employees where id='" . makeSafe($data['assigned_member']) . "'");
		
		mail(
			$email,
			'A new task has been assigned - ' . $data['task'],
			$mail_body,
			"From: [email protected]\r\n" .
			"MIME-Version: 1.0\r\n" .
			"Content-type: text/html; charset=iso-8859-1\r\n"
		);
		
		return TRUE;
	}
I would like to change it using sendmail because the emails are not being received by most tests that I have done. I have tried using examples on this forum but I haven't got it to work. I have set SMTP in admin and it is working as expected.

Thanks Bruce.

pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Use sendmail

Post by pfrumkin » 2020-12-07 17:44

I send email from a .php file. I have a redirect to this from a hook, then later in the script I redirect back to the tableview. Maybe that is a difference. My code looks to be much the same as yours, but maybe there is some subtle difference

Code: Select all

  $lf = "<br/>";
  $home = sqlvalue("SELECT Home FROM ConfigKeys LIMIT 1");
  $to = sqlvalue("SELECT Tow_Request_To FROM ConfigKeys LIMIT 1");
  $from = sqlvalue("SELECT Tow_Request_From FROM ConfigKeys LIMIT 1");
  $subject = sqlvalue("SELECT Tow_Request_Subject FROM ConfigKeys LIMIT 1")." - ".$line_num;
  $body = sqlvalue("SELECT Tow_Request_Body FROM ConfigKeys LIMIT 1").
        $lf.$lf.$record.
        $lf.$lf."To view this record please go to: ".$lf.
        $home.
        "Vehicles_view.php?SelectedID=$recID".$lf;
  $headers =  'MIME-Version: 1.0' . "\r\n"; 
  $headers .= 'From: ' . $from . "\r\n";
  $headers .= 'Bcc: ' . $from . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  @mail($to, $subject, $body, $headers);
Content really isn't relevant of course. My notification contains a link to a specific record that I pass in the querystring.

Hope it helps. Good luck.

~Paul

User avatar
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Re: Use sendmail

Post by bruceholt » 2020-12-08 07:29

Thanks Paul,

I currently have it set up to use PHP mail but they get blocked on most servers which is why I am trying to use SMPT using SSL.

I'll have to do some more digging.

Thanks Bruce

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Use sendmail

Post by jsetzer » 2020-12-08 07:53

Hmmmm, I am a little bit confused because you have written that you want to use SMTP, configured in AppGini Admin Area, but I can see from your code that you are not using the built-in AppGini sendmail-function but the default PHP mail-function which does not know of AppGini's configured SMTP parameters. Maybe you are mixing it up or maybe I got it wrong.

If you want to use SMTP (which makes sense due to SPAM blocking) and if you have set up AppGini's SMTP parameters correctly (which can be verified from Admin area), the syntax for using AppGini's SMTP sendmail-function is different, I think:

You need to call the function named sendmail (from incFunctions.php), not mail, and pass an associative array like this (not tested, there may be typos):

Code: Select all

$mail = [
"to" => "[email protected]",
"name" => "recipent display name",
"subject" => "your subject",
"message" => "your message"
];
sendmail($mail);
If I were you, I would try sending the mail with a simple e-mail body as a start. Only if that works, I would take care of the more complex html body.

To prevent blocking by mail servers, the SMTP configuration of the sender address should match the actual domain: suppose your SMTP server is called smtp.mydomain.net
Bad sender: [email protected]
Good sender: info@mydomain.net
There are more anti-spam rules but the valid sender is very important.

I hope that helps!
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Re: Use sendmail

Post by bruceholt » 2020-12-19 19:43

Hi Jan,

Sorry for the late reply. Thanks for your input. I will do some testing.

Regards, Bruce

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Use sendmail

Post by jsetzer » 2020-12-19 22:04

Maybe this structured step-by-step guide can help, too:
https://appgini.bizzworxx.de/appgini/st ... ing-mails/
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Re: Use sendmail

Post by bruceholt » 2020-12-20 06:49

Thanks Jan, I'll take a look.

Post Reply