Email record

meanster99

Well-known member
Hi all,

Does anyone know how to set up a button or function that will allow me to email the details of a single record to the email adderess contained in the email field of the record?

I have only just got back into this Dadabik project, having found it an equal to PHPMaker (you get far more control and support with Dadabik than you do with PHPmaker, although normally you have to rely on the support of the amazing Dadabik support staff here (especially Debbie et al!) to ensure you can get things to work. PHPmaker is limited due to the fact you can't normally change the code or request changes). I would pay for Dadabik over PHPMaker anytime...

Thanks in advance,

Matt
 

eugenio

Administrator
Staff member
Dear Matt,
thanks for your appreciation!
The modification you need shouldn't be too difficult to implement. Where the button should be, at the end of the edit form?
 

meanster99

Well-known member
Hi Eugenio,

Using Dadabik v4.3 (fairly modified already though)
Apache version: 2.2.22
PHP version: 5.3.13
MySQL version: 5.1.63-cll
Server Operating system: linux

Sorry for the long delay in replying. I moved to Australia last month and have only just got back into my Dadabik project again.

I am still keen to implement this modification. However, it has changed slightly. Let me explain what I need (both emails are essentially requiring the same function, just slightly different content in the emails).

1) I would like to be able to send a receipt email to the email address within the record. The receipt is a standard email with a few of the record fields to be included (i.e. name, price, ref number etc). The button for this could be at the end of the edit form or at the beginning of the results page. So once I manually update a field from 'unpaid' to 'paid' I would like to be able to send the email by clicking a button (I'm not overly concerned with making the button conditional - it can be present at all times).

2) I would like to be able to send a 'late payment reminder' email to the email address within the record. Again, it's a standard email with a few of the record fields to be included. This is OK to be a manual process - payments are checked everyday so if one has not been received the user can just press the button to send the reminder email to the client. Again the button could be at the end of the edit form or the beginning of the results page. Again, not too concerned with it being conditional - it can appear at all times if easier.

I only have a very basic understanding of php (normally cut and paste stuff!), but am a fairly quick learner, so although I don't expect you to write the function for me, please go easy on me (provided you still have the time to help!).

Many thanks in advance,

Matt.
 

meanster99

Well-known member
Hi Eugenio/Debbie/Anyone,

I don't wish to seem impatient but can anyone at least point me in the right direction for how I add these 2 email buttons to the edit form? I probably only need to know the function I need to change, what php I need to make it email and I can probably work out how to compose the email myself, but ANY help will be much appreciated (didn't want to have to pull the 'Hero Donator' card but if it gets me help...)

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
Sorry, I was not following this as I didn't want to duplicate what Eugenio was doing.

The automatic mail stuff is found in the section starting with if ($enable_update_notice_email_sending === 1) {. In there, it references the function used to build the email contents.

Gathering pieces from this section, you should be able to build the email code. If I have some time over the next while, I'll revisit - for some reason, I just cannot get my head wrapped around this today.
 

meanster99

Well-known member
Hi Debbie,

Thanks for the pointer. Unfortunately, I am nowhere near competent enough in php to build the email code I need. I don't even know where to begin really. I had hoped that looking at the code you suggested would help, but unfortunately it's just made me realise I won't be able to make this modification without substantial help.

Hopefully, someone will come along and provide that help, as they have in the past (normally you!), but if not, perhaps I could make another donation to get the help I wanted?

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
Ok ... try this.
I confirmed the code works and the links work - I cannot confirm the mail function works as I'm not at my servers today. Load the code and test it out. Once you start testing, you can then look at what modifications you will need to make. If you don't like the resulting email, either modify the underlined function in the green addition code below OR copy that function and create your own with a different name).

Find this in index.php on or about line 977:
[pre]
else{

echo $form;

reset($fields_labels_ar);
foreach($fields_labels_ar as $field_label){ // for each field of this table
[/pre]

Insert green code below (bold parts are the ones you will want to cusomize):
[pre]
else{

echo $form;

// build the reminder / overdue message
$reminder_email = build_insert_update_notice_email_record_details($fields_labels_ar, $res_details);

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';

foreach ($update_notice_email_to_recipients_ar as $update_notice_email_to_recipient){
$to_addresses .= $update_notice_email_to_recipient.', ';
} // end foreach

$to_addresses = $fields_labels_ar["email_field_from_table"]; // enter the field which contains the email address you want to send to
//substr($to_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_cc_recipients_ar as $update_notice_email_cc_recipient){
$cc_addresses .= $update_notice_email_cc_recipient.', ';
} // end foreach

// you could put your email address in the $update_notice_email_cc_recipients_ar part of config.php OR hardcode your email address here if you want to get a cc on these mailings - leave as is / blank in config if you don't want cc on email
$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_bcc_recipients_ar as $update_notice_email_bcc_recipient){
$bcc_addresses .= $update_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses;
} // end if

// modify the text below which will go into your subject line:
if (isset($_GET["receipt"]) && ($_GET["receipt"] != '')) {
$msgset = $_GET["receipt"];
$msgtype = 'Your Receipt';
//mail(TO, SUBJECT, MESSAGE, HEADERS(CC, BCC, etc));
//make adjustments as needed to the mail line:
if (mail($to_addresses, $msgtype.' - '.$table_name.' - '.$normal_messages_ar['new_update_executed'], $reminder_email, $additional_headers)) {
$msgsent = ' - Receipt email sent';
}
} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {
$msgset = $_GET["latepmt"];
$msgtype = 'Payment Overdue';
if (mail($to_addresses, $msgtype.' - '.$table_name.' - '.$normal_messages_ar['new_update_executed'], $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
}
} else {
$msgset = '';
$msgtype = '';
$msgsent = '';
}

txt_out('<ul type="disc"><b>Email actions on current record:</b><ul type="disc">');
//receipt email link
txt_out('<li>[url='.$_SERVER[]Send Receipt Email[/url]'.$msgsent);
// late payment reminder
txt_out('<li>[url='.$_SERVER[]Send Late Payment Reminder Email[/url]'.$msgsent);
txt_out('</ul></ul>'); // closes the list


reset($fields_labels_ar);
foreach($fields_labels_ar as $field_label){ // for each field of this table
[/pre]

Forgot to show what it will look like in the form:
[Save / Update Button]

Email actions on current record:
  • Send Receipt Email
  • Send Late Payment Reminder Email
 

meanster99

Well-known member
Hi Debbie,

Wow, that's amazing! Thank you so much. It's after 4am here (I'm a Brit living in Australia) and I really need to get some sleep, so will tackle this properly in the morning.

However, I was so excited to get a response (I know, it's sad the things that excite me at 4am!) , I added this code to index.php just to see if I could get it to work. Unfortunately, it's not sending any emails yet.

I followed your instructions within the code, but it's quite difficult to see the bold in the green, so having to use firebug to check they are bolded!

The links display fine at the end of the edit form. Perfect. However, still haven't got the emails to send. Have updated the email field with my table fieldname ('email') - I only have to put the fieldname and not ["tablename.fieldname"], right?

I see you have bolded the code: '.$table_name.' - '.$normal_messages_ar['new_update_executed'] that appears in 2 places. Do I need to add my table name instead of '.$table_name.' ? (Table name is "customer_booking_form"). I did try it both ways (replacing with my table name and leaving it as is) but neither worked.

I also noticed that whichever email link I press, both links then get 'Receipt email sent' displayed next to them, even though I may have only pressed one of the links.

Also the actual URLs of the links that are being produced at the end of the edit form for my test record (250454) are:

For late payment: http://www.mydomain.co.uk/database/index.php?table_name=customer_booking_form&function=edit&where_field=ID&where_value=250454&receipt=1&latepmt=1&receipt=1&latepmt=1&latepmt=1

For receipt: http://www.mydomain.co.uk/database/index.php?table_name=customer_booking_form&function=edit&where_field=ID&where_value=250454&receipt=1&latepmt=1&receipt=1&latepmt=1&receipt=1

which seems to triplicate or duplicate the receipt=1 and latepmt=1 in the URL. This doesn't look like it should be doing this?

Also, ideally, I would only like these buttons to appear on records for one table only (customer_booking_form), as they appear on all tables currently. And I will also need to add a few extra fields into the email message body, so not too sure how I adapt the code for that.

Anyway, as I say, it's bedtime for me, but I really appreciate your help Debbie and I look forward to anything further you have to add. Many thanks for taking the time to do this much for me, I really can't thank you enough...

Cheers,

Matt
 

meanster99

Well-known member
Hi Debbie,

Not been able to get the whole thing working yet but did add the if statement below (in bold) to limit these buttons to the table I wanted, so don't need help with that bit at least.

else {

echo $form;

if ($table_name == "customer_booking_form") {

***All your code in green***

}// endif

So now I just need to work out why the emails are not being created/sent, and why the links when clicked aren't displaying the correct confirmation message (i.e.both say 'Receipt email sent' regardless of which link is pressed, when the late payment should say 'Late Payment email sent').
 

meanster99

Well-known member
Tried to edit the last paragraph of the previous post, but wouldn't let me. Paragraph should read:

So now I just need to work out why the emails are not being created/sent, and why the links when clicked are displaying the confirmation message for whichever link was clicked first (i.e.both say 'Receipt email sent' if that link is pressed or both say 'Late Payment email sent' if that link is pressed).
 

DebbieS

DaDaBIK Guru
OK ... change the links to read:
[pre]
txt_out('<li>

Basically that (including the $msgtype variable) will write this to the subject line (using your table name):
Your Receipt - customer_booking_form - New update executed
$msgtype - $table_name - $normal_message.........

I would change that to read $msgtype.' - 'Order '.$res_details["orderNOfield"]... or something like that which would be meaningful to the recipient.
 

DebbieS

DaDaBIK Guru
I've been playing around with this and I cannot get it to work properly. I'm thinking it may be easier to implement if you add another field to your database table (eg status, with the values '', 'Receipt Sent', 'Late Notice Sent'). Then turn on the following option in your config.php:
$enable_update_notice_email_sending = 1

Next, we will update that part of the case "update" in index.php (changed / added parts in bold - edit the mail() line as required):

[pre]
if (($enable_update_notice_email_sending === 1) && ($table_name == "customer_booking_form")) {

$sql = build_select_part($fields_labels_ar, $table_name);

$sql .= " WHERE ".$quote.$table_name.$quote.".".$quote.$where_field.$quote." = '".$where_value."'";

// execute the select query
$res_details = execute_db($sql, $conn);
$emaildet = fetch_row_db ($email_details);
$emailto = $emaildet['textemailfield']; // change to your email field name here

if ($emaildet["status"] != '') { // receipt / late pmt selected in the update of the record

// modify the text below which will go into your subject line:
if ($emaildet["status"] == 'Receipt Sent') {
$msgtype = 'Your Receipt';
} elseif ($emaildet["status"] == 'Late Notice Sent') {
$msgtype = 'Payment Overdue';
} else {
$msgtype = '';
}


// build the update notice message
$update_notice_email = build_insert_update_notice_email_record_details($fields_labels_ar, $res_details);

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';

foreach ($update_notice_email_to_recipients_ar as $update_notice_email_to_recipient){
$to_addresses .= $update_notice_email_to_recipient.', ';
} // end foreach

$to_addresses = $emailto;//substr($to_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_cc_recipients_ar as $update_notice_email_cc_recipient){
$cc_addresses .= $update_notice_email_cc_recipient.', ';
} // end foreach

$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_bcc_recipients_ar as $update_notice_email_bcc_recipient){
$bcc_addresses .= $update_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses;
} // end if

mail($to_addresses, $msgtype.' - '.$db_name.' - '.$table_name.' - '.$normal_messages_ar['new_update_executed'], $update_notice_email, $additional_headers);
} // run the update email notice only if Receipt/Late Pmt selected!!!

[/pre]

To modify what the email includes, just look at and modify the function underlined in the code above.

This method will not display anything on screen, however, I DO have this background mailing working in one of my installs at work and it does not fail. As a check, I normally set my email update cc field in my config.php file to my own address so I get the update emails also. The nice thing about this is you have a record of the email and can forward it as required or follow up on it.

If you try this and it works, then you will need to comment out the original code that I supplied (surround with /* code */).
 

meanster99

Well-known member
Hi Debbie,

Yes, I couldn't get this to work either, so I am open to new suggestions. However, I currently already use the email notifications for updates (so $enable_update_notice_email_sending = 1), so if I implement your new solution, this will mean I can no longer receive the standard update notifications, doesn't it? Presumably, I would also have to remove the current email address in config.php and make it empty again, i.e.:

$update_notice_email_to_recipients_ar[0] = ' ';

So with this new solution, once the user changes the value for field 'status' (which I shall probably call 'pay_email_status' - already have a field called 'status'!) and then presses 'Save Record', the email related to that status will then be sent and whoever I set as the cc and bcc addresses in config will also receive the email?

Thanks,

Matt
 

meanster99

Well-known member
Hi Debbie,

Just had another thought - you say 'To modify what the email includes, just look at and modify the function underlined in the code above.', but this produces only one email doesn't it? I need 2 different emails sending out. Just looking at the function for this makes my head spin - don't know how I would edit the code for one email, let alone 2.

I was so hopeful of your initial suggestion and am now starting to worry I will never get this functionality working. Unless you don't think it's that big a task to send 2 different emails?
 

DebbieS

DaDaBIK Guru
Matt

I'll answer your questions best I can ...

1.....
I currently already use the email notifications for updates ....
Well this changes things then. If you're already using the auto-email functionality, then the manual link/button will need to be revisited. If it was set up to send email based on the email field, then it would send it everytime that record was updated - even if the update was not to that field. I'll have to look at the other code again to see if I can get it to work.

2.....
Unless you don't think it's that big a task to send 2 different emails?
It really depends on the amount of change you need. Details, details, details. Who knows?

Before that, we should get the email links working though .....

Try this code to test the mail functionality when clicking on the links (add part NOT in bold and update the red items to match what you have in your own DB):
[pre]
echo $form;

if ($table_name == 'customer_booking_form') {
$email_details = execute_db($sql, $conn);
$emaildet = fetch_row_db ($email_details);
$emailto = $emaildet['textemailfield'];
$orderno = $emaildet['orderNoFld'];
// build the reminder / overdue message - just using this to test with for now
$reminder_email = 'Testing reminder email variable';//build_insert_update_notice_email_record_details($fields_labels_ar, $email_details);

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';

$to_addresses = $emailto; // this is from the db query above (below echo $form;) - use your own email to test email sending

foreach ($update_notice_email_cc_recipients_ar as $update_notice_email_cc_recipient){
$cc_addresses .= $update_notice_email_cc_recipient.', ';
} // end foreach

// you could put your email address in the $update_notice_email_cc_recipients_ar part of config.php OR hardcode your email address here if you want to get a cc on these mailings - leave as is / blank in config if you don't want cc on email - this you will need to decide how you want to use ...
$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_bcc_recipients_ar as $update_notice_email_bcc_recipient){
$bcc_addresses .= $update_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses;
} // end if

// modify the text below which will go into your subject line:
if (isset($_GET["receipt"]) && ($_GET["receipt"] != '')) {
$msgset = $_GET["receipt"];
$msgtype = 'Your Receipt';
//mail(TO, SUBJECT, MESSAGE, HEADERS(CC, BCC, etc));
//make adjustments as needed to the mail line:
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Receipt email sent';
}
} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {
$msgset = $_GET["latepmt"];
$msgtype = 'Payment Overdue';
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
}
} else {
$msgset = '';
$msgtype = '';
$msgsent = '';
}

txt_out('<ul type="disc"><b>Email actions on current record:</b><ul type="disc">');
//receipt email link
txt_out('<li>[url='.preg_replace(]Send Receipt Email[/url] - '.$to_addresses.$msgsent);
// late payment reminder
txt_out('<li>[url='.preg_replace(]Send Late Payment Reminder Email[/url]'.$msgsent);
txt_out('</ul></ul>'); // closes the list
} // end code for email links in customer_booking_form table edit pages

reset($fields_labels_ar);
foreach($fields_labels_ar as $field_label){ // for each field of this table

[/pre]

Try this to confirm the email gets sent (to test, just put your email address in the $to_addresses field). Once we can confirm that the basic email works, then send me what you want to have in each email message (include field names you need included) and we can look at customizing the email message being delivered.

Of course, you could still add the Receipt sent and Late Pmt email sent to the list of status messages (or a new field) if you want to track that.
 

meanster99

Well-known member
Hi Debbie,

As always, you have come through again! This is amazing, thanks. Emails send OK. I have added additional headers to your code - From, Reply-to, PHP version (it works still but I am hoping my additions don't affect the next step - compiling the actual emails - feel free to correct or confirm!). The full code, with my additions in red:

[pre]
if ($table_name == 'customer_booking_form') {
$email_details = execute_db($sql, $conn);
$emaildet = fetch_row_db ($email_details);
$emailto = $emaildet['email'];
$orderno = $emaildet['ID'];
$from_address = 'bookings@mydomain.co.uk';

// build the reminder / overdue message - just using this to test with for now
$reminder_email = 'Testing reminder email variable';//build_insert_update_notice_email_record_details($fields_labels_ar, $email_details);

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';
// not sure if I was also to add the $from_address above
$to_addresses = $emailto; // this is from the db query above (below echo $form;) - use your own email to test email sending

foreach ($update_notice_email_cc_recipients_ar as $update_notice_email_cc_recipient){
$cc_addresses .= $update_notice_email_cc_recipient.', ';
} // end foreach

// you could put your email address in the $update_notice_email_cc_recipients_ar part of config.php OR hardcode your email address here if you want to get a cc on these mailings - leave as is / blank in config if you don't want cc on email - this you will need to decide how you want to use ...
$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '

foreach ($update_notice_email_bcc_recipients_ar as $update_notice_email_bcc_recipient){
$bcc_addresses .= $update_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses;
} // end if

if ($from_address != '') {
$additional_headers .= "From: ".$from_address."\r\n".
"Reply-To: ".$from_address."\r\n".
"X-Mailer: PHP/".phpversion();
} // end if



// modify the text below which will go into your subject line:
if (isset($_GET["receipt"]) && ($_GET["receipt"] != '')) {
$msgset = $_GET["receipt"];
$msgtype = 'Your Receipt';
//mail(TO, SUBJECT, MESSAGE, HEADERS(CC, BCC, etc));
//make adjustments as needed to the mail line:
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Receipt email sent';
}
} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {
$msgset = $_GET["latepmt"];
$msgtype = 'Payment Overdue';
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
}
} else {
$msgset = '';
$msgtype = '';
$msgsent = '';
}

txt_out('<ul type="disc"><b>Email actions on current record:</b><ul type="disc">');
//receipt email link
txt_out('<li>[url='.preg_replace(]Send Receipt Email[/url] - '.$to_addresses.$msgsent);
// late payment reminder
txt_out('<li>[url='.preg_replace(]Send Late Payment Reminder Email[/url]'.$msgsent);
txt_out('</ul></ul>'); // closes the list
} // end code for email links in customer_booking_form table edit pages

[/pre]

(I did take your earlier advice and add a new field to the DB for 'emailstatus' for tracking purposes. I don't want to take liberties with you, but would it be a big mod to get this field to update automatically? I can live with the user doing it manually, but you know users and if they forget, we could annoy customers!)

OK, so the sending email part is working, so next we just need the email content with included fields:

1) Late Payment email format:

Dear [firstname] [surname],

Our records indicate...blah blah blah...outstanding payment of [booking_fee]...blah blah blah

Please click the following link to make payment - I want to add an absolute, same for everyone, link in here. It's not a DB field, just may need help with how to output links

Regards,

Blah blah blah.



2) Receipt email format:

Dear [firstname] [surname],

Blah blah blah

Receipt No: [ID]

Service: [service_required]

Total Price: [total_price]

Booking Fee: [booking_fee] (Received with thanks: insert todays date)

Remaining Balance: [balance]

Blah blah blah

Regards,

Blah blah blah.


Once again, many thanks for all your help Debbie - you really are a star and I'm just sorry I'm need my hand holding so much!

Cheers,

Matt
 

DebbieS

DaDaBIK Guru
Setting up the emails with the content you've outlined is not difficult at all. Using the same format at the beginning of the inserted code (where you added the email, ID and from address, add the other fields you want to capture:

[pre]
$emailto = $emaildet['email'];
$orderno = $emaildet['ID'];
$efirstname = $emaildet['firstname'];
$esurname = $emaildet['surname'];
$esvcreq = $emaildet['service_required'];
$etotprice = $emaildet['total_price'];
$ebookfee = $emaildet['booking_fee'];
$ebalance = $emaildet['balance'];
[/pre]

For the emails themselves, move the $reminder_email variable from it's current position into each IF/ELSE section appropriately. If you are formatting your email as HTML, then just add the appropriate coding to the text below:

[pre]
if (isset($_GET["receipt"]) && ($_GET["receipt"] != '')) {
$msgset = $_GET["receipt"];
$msgtype = 'Your Receipt';
$reminder_email = '
Dear '.$efirstname.' '.$esurname.',

Blah blah blah

Receipt No: '.$orderno.'
Service: '.$esvcreq.'
Total Price: '.$etotprice.'
Booking Fee: '.$ebookfee.' (Received with thanks '.date("d F Y", time()).')
Remaining Balance: '.$ebalance.'

Blah blah blah

Regards,

Blah blah blah';

//mail(TO, SUBJECT, MESSAGE, HEADERS(CC, BCC, etc));
//make adjustments as needed to the mail line:
if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Receipt email sent';
// update the record
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'receipt');

}
} elseif (isset($_GET["latepmt"]) && ($_GET["latepmt"] != '')) {
$msgset = $_GET["latepmt"];
$msgtype = 'Payment Overdue';
$reminder_email = '
Dear '.$efirstname.' '.$esurname.',

Our records indicate...blah blah blah...outstanding payment of '.$ebookfee.'...blah blah blah

Please click the following link to make payment:
http://www.domain.com/paymentlink.php
should work just by putting in the URL, however, to be sure, you can insert it like so:
http://www.domain.com/paymentlink.php


Regards,

Blah blah blah';

if (mail($to_addresses, $msgtype.' - '.$orderno, $reminder_email, $additional_headers)) {
$msgsent = ' - Late Payment email sent';
// update the record (emailstatus is your email status db field name)
update_sent('emailstatus', $table_name, $table_internal_name, $where_field, $where_value, 'latepmt');

}
} else {
$msgset = '';
$msgtype = '';
$msgsent = '';
$reminder_email = '';
}
[/pre]

I did take your earlier advice and add a new field to the DB for 'emailstatus' for tracking purposes. I don't want to take liberties with you, but would it be a big mod to get this field to update automatically? I can live with the user doing it manually, but you know users and if they forget, we could annoy customers!

It is possible by adding another function and then calling that function within the if(mail... parts.

Add the function to each IF/ELSE part into the code above (the blue lines).

Insert this function after update_record function in business_logic.php (change wording or field value you need as required):
[pre]
// update the emailstatus field of the record after email successfully sent
update_sent($emailstatus, $table_name, $table_internal_name, $where_field, $where_value, $emailtype);

if ($emailtype == 'receipt')) {
$statusmsg = 'Receipt Sent';
} elseif ($emailtype == 'latepmt') {
$statusmsg = 'Late Notice Sent';
} else {
$statusmsg = '';
}
$sql = "UPDATE ".$quote.$table_name.$quote." SET ".$quote.$emailstatus.$quote." = '".$statusmsg."' WHERE ".$quote.$where_field.$quote." = '".$where_value."'";

display_sql($sql);

// update the record
$res_update = execute_db($sql, $conn);

} // end function update_sent
[/pre]
 

meanster99

Well-known member
Hi Debbie,

That is absolutely fantastic and works beautifully. So many thanks.

The only problem is with the update_sent function. I kept getting errors. After removing an extra bracket on line 3 of your original code and adding the word 'function' and a curly bracket to the 2nd line, I am left with this:

function update_sent($emailstatus, $table_name, $table_internal_name, $where_field, $where_value, $emailtype) {

if ($emailtype == 'receipt') {
$statusmsg = 'Receipt Sent';
} elseif ($emailtype == 'latepmt') {
$statusmsg = 'Late Notice Sent';
} else {
$statusmsg = '';
}
$sql = "UPDATE ".$quote.$table_name.$quote." SET ".$quote.$emailstatus.$quote." = '".$statusmsg."' WHERE ".$quote.$where_field.$quote." = '".$where_value."'";

display_sql($sql);

// update the record
$res_update = execute_db($sql, $conn);

}// end function update_sent

However, when I send the email (click the link), email gets sent fine but the field is not getting updated - I get the following error:

Fatal error: Call to a member function Execute() on a non-object in /home/y09dann/public_html/database/include/db_functions_adodb.php on line 107

My field names are the same as yours. I notice you reference $emailstatus, but that isn't declared anywhere is it? Forgive my ignorance...

Thanks,

Matt
 

DebbieS

DaDaBIK Guru
Well, yes, actually, they do. My apologies for the booboo on the function section - my bad.

When a variable is listed in the function ***() brackets, it does not need to be declared in a global line. When using a variable that is not in the function call, you do need to call / reference them.

So, right after the opening function line, add this:

[pre]
global $conn, $quote;
[/pre]

Those are the two variables that are missing from the new update_sent function.
Let me know if that resolves things.
 
Top