Topic: email single invoice to multiple recipients

Hello,

I read the wiki, but I am confused about emailing contacts.  I have customers where the invoice needs to be sent to more than one individual.  So far I have been able to work around that by simply entering a comma-separated list of email addresses into a single contact.  Today, I discovered I have a customer whos domain name is too long to enter two addresses into one field.

So I created a 2nd contact and set the assignment to general.  I understood the wiki to mean that invoices would be sent to all contacts with assignment of general or invoice.  But when I create a direct invoice and email it, it only sends to the 2nd contact's address.  So I added the 2nd contact in the branch, then  I did another invoice, but it also sends to only the 2nd contact.

Clearly I am missing something, but I don't understand what it is.  Any chance someone can enlighten me?

Re: email single invoice to multiple recipients

Here i have an article for you to understand it well. Also the image explains you the way to setup the multiple recipients.

http://www.kvcodes.com/2014/10/send-invoice-multiple-recipient-frontaccounting/

Subscription service based on FA
HRM CRM POS batch Themes

Re: email single invoice to multiple recipients

Thanks for your reply.

when I added the 2nd contact to the branch, I had done what you did up to step 5.  After that, I selected the existing contact, cloned it, and then changed the email address.  that did give me a list of two contacts, each with different email addresses.  But it still only sent to one address.

I have deleted the clone and followed your instructions; unfortnately I have no more invoices to send them right now, but I will test this in the near future...

Re: email single invoice to multiple recipients

kvvaradha:

Thank you for your instructions.

I have set up another customer following the 8 steps you outlined.  I think your instructions are clear and I (think I) understand what it supposed to happen. 

Now that it is setup, when I look under customer branches, select the customer, I see one item in the list with a short name, tax group, etc., and a single email address.  I then click the edit icon, select the contacts tab, and I can see both contacts, each with their individual email, exactly as the picture you setup shows it should be.  Then I go to customer reports to email an invoice, and it gets sent only to the first contact. 

Do I need to delete the contacts under manage customers or something to make this work?

Re: email single invoice to multiple recipients

I can't answer your question, but I would just have extended the field where you added the email addresses.

Re: email single invoice to multiple recipients

Hi tm,

Thank you for your response.

In the testing I have been doing, it starts to look like separating the email addresses with a comma isn't actually working as expected.  I have been phoning around to find who has gotten invoices and who hasn't, and it seems the mail does not actually send when the email field contains comma separated values.

hence why I am working on getting this thing figured today....

Re: email single invoice to multiple recipients

By good fortune I manage a couple mail servers, and I have been rootin' through the logs trying to figure out what exactly is up with how FA deals with mail.  But as of now I can't decide if I am more confused or less confused than I was.  I am pretty comfortable with email, but I am not sure what this php mailer is up too...

First, I have found that while the from header of delivered mails contains my address, the actual sender of the mail is the username that apache is running under.  by way of example, I use mpm-itk on my server and run frontaccounting under user fa, so bounces go to fa@domain.tld instead of my mail address.

I also found that you cannot use multiple email addresses in one field, whether separated by semi colon or comma.  FA will say that it is sent, and in the strictest sense that is true, however, the mail will be bounced back as malformed.  Though I did find it quite consistently successfully delivered the mail to the first recipient in the list when using semi colons.  Having dealt with emailing from Perl scripts, I suppose this isn't too big of a surprise.

I found consistently that when multiple contacts exist within a branch, regardless if they all have the same contact category, only one of the contacts will have mail sent to it.  I found consistently the same with customer contacts.  I found that deleting all contacts from the branch and using only customer contacts was the same as deleting all contacts from the customer and using only contacts from the branch.  I found that setting a contact's contact category to invoice will cause it to be prioritized over general contact category in both cases.

I can demonstrate through smtp sessions on the recipient mail server and through the exim logs on the server where front accounting resides that no attempt is made to deliver mail to the 2nd recipient.  it is not that the mail fails to send, it is that it is not sent, at least as far as I can tell.

I have found that while only one contact will be sent too, the auto-BCC function works perfectly. 

I think I have tried all combinations of contact category, branch contact, customer contact, and under no circumstance can I get FA to send two emails at the same time.  At this point, the only successful way I have sent one invoice to multiple recipients is to save the invoice as a pdf and attach it manually.

Any suggestions as to further things I could try would be most welcome.

BTW; when a mail gets sent, it starts with "Dear ," and in my experimenting it gets populated with the last name of the recipient.  For example, if I populate a contact with first name Bob and last name Miller, the mail will be addressed Dear Miller, .  Not sure if this is the intended design, or if this indicates I have a misconfiguration somewhere; and if I do have a misconfiguration, perhaps it is related to my inability to send multiple mail?

8 (edited by apmuthu 11/06/2014 07:19:54 am)

Re: email single invoice to multiple recipients

The EMail Name issue is discussed in another thread.

Chief Dev Response

What is proposed as solution for email title issue, will solve problem
for some people and will make things worse for others. The person titles
and order of names in official post exchange is just matter of local
habits. I have decided to leave this as is for now. Proper solution
would require some email template system to be implemented, for which
work we have no free resources now. But anybody interested can customize
the source code in this point with minimal effort.

Re: email single invoice to multiple recipients

acknowledged, thank you for pointing me at that.  I hadn't really investigated that part, just thought I would add it in case it was relevant, which I see now it wasn't....

Re: email single invoice to multiple recipients

I am sorry to resurrect an old topic, but this starts to turn into a sliver in my toe. 

I never did solve the problem at the end of last time I worked on it, instead I was just manually saving the invoices and mailing them.  But now a good chunk of my customers are loaded into the program and I simply don't have enough RAM in my brain to keep track of who needs multiple recipients. 

I have been through the diagram provided by kvvaradha (and thank you for providing that) multiple times across multiple customer additions now.  The only discrepancy between that diagram and what I enter is that I cannot enter just an email address, I must also enter a name and reference.  But in no customer where two contacts are setup does it ever send two emails.

This afternoon I started by verifying all my previous observations and ensuring that any changes I have made are not the culprit.  turns out all my changes are limited to the doctext.inc and header2.inc files.  I also upgrade from 2.3.21 to 2.3.22.  So my environment is clean, and the problem is still that FA is sending only one mail to one recipient.

I am running FA on a debian server with exim4.  Exim4 is configured to use my main mail server as a smarthost.  One observation I can add to previous ones is that the php mail() function uses sendmail, which in this case is linked to exim4, which in turn creates an smtp session to may mail server.  I was unable to find a way to get mail() to send via another route, nor from here was I able to find any proof that FA was sending to multiple recipients.

I curse myself that after all my time playing with linux servers I haven't ever become truly proficient in php.  but I got to poking around, and I believe that class.mail.inc and pdf_report.inc are the two files, or at least two of the files, that are responsible for determining the recipients and mailing to them.

I am not clear if the secondary recipients of the mail are supposed to be cc'd, or if an individual mail is supposed to be created for each recipient.  I put a print_r statement in the foreach loop at the bottom of class.mail.inc; $this->header shows no CC entry, and $this->to contains only one entry in the array.  If I put my print_r statement in pdf_report.inc at around line 1000, $this->contactData also only shows one entry in the array.  I have dumped several of the variables but I can find nothing that  indicates FA is finding a 2nd recipient.

Maybe I am not checking the right variables, or in the right place, but this seems to confirm my suspicion that FA is indeed only sending one email to one recipient.  But I am still no closer to figuring out how to fix it.  If this is working for everyone else, then I must have a configuration error in my apache or php or something, but what could it be?  As before, suggestions would be greatly appreciated....

Re: email single invoice to multiple recipients

See if the function that gets the email addresses returns a scalar / one address only.

Also see if your sys_prefs table has this entry.

Also check if this post is relevant to this issue.

You may have to manually hard code in 2 or more email addresses into the routine that emails out and see if the emails sent actually get through php in the first place.

Some issues discussed in this post may be relevant as well.

Re: email single invoice to multiple recipients

Thank you so much for your reply.

apmuthu wrote:

See if the function that gets the email addresses returns a scalar / one address only.

Okay, so I am not 1000% sure exactly which function that is.  I am still swimming in code. 

But; in mail.class.inc, the email address also shows as a single element in an array, both in the to($mail) function at the top, and in the foreach at the bottom.  so by my understanding of the term, not a scalar

in pdf_report.inc there is a function called End, in it I *think* it is supposed to loop through each contact.  I dumped a bunch of stuff in here, but none of it returned anything from my secondary contacts, only from the one.

Poking around some more, in rep103, the branch contacts show all the components of one contact, but do not print anything about the 2nd contact.

Not sure if that is what you mean?  but I will keep poking at the code, maybe an epiphany is waiting to happen...

apmuthu wrote:

Also see if your sys_prefs table has this entry.

Check:

| grn_clearing_act           | glsetup.purchase  | varchar  |     15 | 0                                    |
| bcc_email                  | setup.company     | varchar  |    100 |                                      |
+----------------------------+-------------------+----------+--------+--------------------------------------+
52 rows in set (0.00 sec)

I have also verified that the bcc function works.


apmuthu wrote:

Also check if this post is relevant to this issue.

This post refers to complete failure to send mail, where as I can reliably send.  I checked to make sure I do not have suhosin installed, and I don't.  But I find it interesting that he messed with the format of his headers, I need to stick that idea in my back pocket.  But I don't think the failure to send is due to malformed headers, as I still have no indication that there is a 2nd address to malform them with.


apmuthu wrote:

You may have to manually hard code in 2 or more email addresses into the routine that emails out and see if the emails sent actually get through php in the first place.

I am guessing by this you are suggesting I follow the example in the above post?  shouldn't I verify that FA is actually finding two addresses to send to before I figure out if it can send them?  and doesn't the fact that I can send to single contacts indicate that php is successfully sending mail?  maybe I am missing your point, I am pretty good at overlooking the obvious...


apmuthu wrote:

Some issues discussed in this post may be relevant as well.

The fixes suggested in this post already appear to be implemented in the files on my installation. 

Thank you again for taking the time to respond, I truly appreciate it

13 (edited by apmuthu 02/03/2015 07:39:25 am)

Re: email single invoice to multiple recipients

Currently email is sent using the reporting/includes/class.mail.inc file's email class and that too only using reporting/includes/pdf_report.inc's End() method of the FrontReport class and the function send_reorder_email() in inventory/includes/inventory_db.inc (yes, there is no inventory/includes/db folder in FA 2.3). These wrapper function and method are the only ones that get mail sent in FA.

The email class' methods to(), cc() and bcc() are the ones that add email addresses to the respectively named class properties that get into the mail header before being sent.

Therefore the calling wrapper functions/methods must properly populate the email addresses into the appropriate array argument's to be passed on to the actuator functions in the email class.

Also see a related post.

Re: email single invoice to multiple recipients

Thank you again for your insights, and for the breakdown.  I understand the words, now to see if I can apply the meaning.

I have my test situation where I have my company added as a customer, with one contact being my real info, and another contact going to my hotmail address.  I have tried putting contacts in both the branch and the customer, but as of now I have them just in the branch.

The email class' methods to(), cc() and bcc() are the ones that add email addresses to the respectively named class properties that get into the mail header before being sent.

If I understand this correctly, then I should be able to capture all email addresses that FA is adding from within these functions.

In class.mail.inc I adjust the to() function and the cc() function:

    function to($mail)
    {
        $this->to[] = $mail;
$tooutput = print_r($this->to, true);
file_put_contents('/var/www/fa.computerisms.ca/tmp/tooutput.txt', $tooutput, FILE_APPEND);

    }
    function cc($mail)
    {
        $this->cc[] = $mail;
$ccoutput = print_r($this->cc, true);
file_put_contents('/var/www/fa.computerisms.ca/tmp/ccoutput.txt', $ccoutput, FILE_APPEND);
    }

I send an invoice, and tail -f the tooutput.txt and the ccoutput.txt files and get:

Array
(
    [0] => Bob Miller <bob@computerisms.ca>
)

so it is reasonable to assume that FA is not picking up the 2nd contact?  I am not a practised coder, so please point out any fallacies in my assumptions.

If I set one contact to invoice and the other to general, the email goes to the one set as invoice.  If I have them set the same, it goes to the first contact.  regardless, only one address ever shows up in the output files I have set up.

Therefore the calling wrapper functions/methods must properly populate the email addresses into the appropriate array argument's to be passed on to the actuator functions in the email class.

Given my above test, I am working on the assumption that the wrapper function is not populating the addresses.  In pdf_report.inc at line 976 I find a line  if ($email == 1), and it seems to my untrained eye this is the block that deals with obtaining the addresses to deliver to and crafting the mail.  I see a line "foreach($contactData as $contact)" which makes me think it should be looping through more than one contact if it exists, but dumping the  $contactData and $contact variables only adds details on one contact to the output file, not the 2nd.  If I dump the $to variable, only one address shows up in my dump file, but I expect two.

I am not sure yet how to work backwards from here.  based on what I have read, I am looking through the rep files, but I don't have any insights to share yet.

I am also still thinking that if this code is working correctly for others, the problem must be in my environment.  But what kind of setting could prevent the code from picking up the 2nd bit of data, maybe an encoding issue in the database?  am I going about this all wrong looking in the code for the answer to the problem?

Re: email single invoice to multiple recipients

I am not clear as to how you populated your contact data.

All email addresses must reside in the #_crm_persons table. All many-to-many linkages are in the #_crm_contacts table where the entity_id will indicate the source table's id (customer => debtor_master table, branch => cust_branch table, supplier, etc). The person_id refers to the id of the crm_persons table. The ERD is self-explanatory.

Now please check your relevant records in the crm_contacts, crm_persons, cust_branch and debtors_master tables and list the sequence you undertook to populate them.

Try to trace the source sql for the $contactData in the pdf_report.inc file.

Manually execute the SELECT sql obtained above and see what gives.

Re: email single invoice to multiple recipients

Thank you *very* much for staying with me on this.  been busy making money so haven't had time to work on this the last couple days.

apmuthu wrote:

I am not clear as to how you populated your contact data.

I am populating my contacts through the web interface.  my primary methodology is using the procedure defined in this link:

http://www.kvcodes.com/2014/10/send-invoice-multiple-recipient-frontaccounting/

Whch was kindly provided early in my investigation of this problem.  I have also consulted the wiki pages on the topic and based on my understanding of branch contacts overriding customer contacts and the invoice vs general type of contact, I have spent many hours trying various combinations of stuff; at first trying to get it working as per the instructions, and then just to try it all since following the instructions weren't working.  I think at this point I have tried everything that the program will let me do.

apmuthu wrote:

All email addresses must reside in the #_crm_persons table. All many-to-many linkages are in the #_crm_contacts table where the entity_id will indicate the source table's id (customer => debtor_master table, branch => cust_branch table, supplier, etc). The person_id refers to the id of the crm_persons table. The ERD is self-explanatory.

Now please check your relevant records in the crm_contacts, crm_persons, cust_branch and debtors_master tables and list the sequence you undertook to populate them.

Try to trace the source sql for the $contactData in the pdf_report.inc file.

Manually execute the SELECT sql obtained above and see what gives.

Everything about the database is within my skill set and understanding.  Tracing the code that does the queries... well, I am learning.  probably I did this the longest hardest way.

I started by examining the tables in mysql; the first thing I noted is I don't have a #_ prefix.  not sure if that matters. I found that both contacts are listed in crm_persons, and it looks to me like crm_contacts has everything linked up correctly.  Next I logged all mysql queries and sent my invoice, and found only one query checking the crm_contacts, and I used that query to figure out there is a function called get_branch_contacts in a file sales/includes/db/branches_db.inc.  From there, I find reporting/rep103.php and reporting/rep107 through rep113 call this function, and thanks to the handy little title at the top of each file (thanks Joe Hunt), 107 is the one to do with invoices.

Phew.

pdf_report uses the $contacts variable to populate $contactData.  the $contacts variable is populated by this line in rep107.php:

$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);

so, dig out the ol' print_r; put one before this line to find the values of $branch, put one after to find the values of $contacts.  $branch correctly dumps the company, $contacts dumps only one contact.  hrm...

On to branches_db.inc.  Here I dump the $sql statement, find the string "&TB_PREF&" in the variable twice.  Not sure why they are there, but clearly they are not being passed to mysql else there would be no contacts.  remove those strings and run the sql statement manually and it returns all contacts.  dump the $results a line or three down, and it returns 2 arrays of data.  Hmm, so the database is being queried for all contacts and is returning them. 

In the next stanza there is an interesting comment:

// select first available contact in $defs order

Sooooo... selecting only the first contact is by design?  Seems to describe what is happening.  Playing with that for a bit, I ended up commenting the whole block starting at line 203 that reads:

if ($results && $default) {

And now when I send the email, it lists at the top of the page that it is sending to both contacts, and even more importantly, email is received at both addresses.  /me does the genius dance...

Surely that block of code is there for a reason, what am I wrecking by commenting it?  it seems like it is making sure it is only sending to contacts that are either invoice or general type?  If that is the case, then I consider this solved, but it may not be a suitable solution for others.

PS, I found this in tmp/errors.log, I produced it by adding a contact to the customer, however the contact did add, and as far as I can tell it is in the database correctly.

[07-Feb-2015 22:35:44 UTC] 0:bob:simple_crud_class.inc:116: key() expects parameter 1 to be array, string given

Anyway, thank you very very much apmuthu, I really appreciate your guidance.

Re: email single invoice to multiple recipients

I figured out what this broke, by commenting the entire block, it now sends mail to every contact.  when a new customer is added, ti creates a contact in the customer and the branch, and now invoices mail to both.  Still; easier at this point to remove all the contacts I dont' want than to not having it mail to all the contacts I do want...

18 (edited by apmuthu 02/08/2015 04:56:05 am)

Re: email single invoice to multiple recipients

In includes/current_user.inc, the following function is defined:

//
//    Search $needle in $haystack or in $haystack[][$valuekey]
//    returns $needle found or null.
//
function array_search_value($needle, $haystack, $valuekey=null)
{
    foreach($haystack as $key => $value) {
        $val = isset($valuekey) ? @$value[$valuekey] : $value;
        if ($needle == $val){
            return $value;
        }
    }
    return null;
}

This function is used by function get_branch_contacts in sales/includes/db/branches_db.inc (this file has another similarly named function prefixed with an underscore that is neither used in the FA core nor in any extension).

At the very end of the function get_branch_contacts we find that only one element is returned because of a return within the loop:

    foreach($defs as $type) {

        if ($n = array_search_value($type, $results, 'ext_type'))

            return $n;
    
    }

If this value is captured in a separate array if it is not empty, then that array would have all the necessary contacts.

This is clearly a bug.

@joe: please update accordingly in FA 2.3 and into FA 2.4.

PS: The error in includes/ui/simple_crud_class.inc is tolerated because of the "@" prefixing the assignment. A check for the variable's existence before assignment would remedy it and it is needed in more than one place in the same function!

Re: email single invoice to multiple recipients

while it is unfortunate there is a bug in the code, I can't say how happy I am to know that it wasn't stupid-user syndrome.  I am also super-happy that I was able to find it and point it out.  Thanks again for your guidance on that apmuthu.

I have two follow-up questions:

1.  I have tried commenting each part(s) of the stanza in the get_branch_contacts function but seems I have to comment the whole thing for email to work.  Can I make a change there to fix this properly, or am I to understand from your reply that the problem is more involved than that?

2.  There is a CC function in class.mail.inc, but I don't find any way to use it.  it seems when a mail is sent to multiple contacts, it make a separate mail for each individual, such that nobody can see if the other recipients got it.  Can this be adjusted?  I have one client who says I need to send by snailmail if it can't, and I am not sure if I remember how to use a stamp wink

Re: email single invoice to multiple recipients

@apmuthu

I am quite busy at present. Will you send me a patch to fix this in release 2.3. Release 2.4 will then be updated when the final merge is done.

Thanks in advance.

/Joe

Re: email single invoice to multiple recipients

About Bob's second question,

When I looked at the code, it seemed that the mail was generated and sent to each recipient (which was of course of little consequence, as there was never more than one).

I would suggest reorganising that bit to first create the message and then loop over the recipients while adding the "To" headers.

(CC is probably best avoided here, as it implies "FYI" rather than "Here's a message for you. Please do something with it". So, who is the "real" recipient, and who gets CC'ed?)

Re: email single invoice to multiple recipients

tm wrote:

I would suggest reorganising that bit to first create the message and then loop over the recipients while adding the "To" headers.

(CC is probably best avoided here, as it implies "FYI" rather than "Here's a message for you. Please do something with it". So, who is the "real" recipient, and who gets CC'ed?)

While multiple To headers would work just fine, CC would actually be more appropriate in most of my cases for the exact reason you state.  Most of my customers are small businesses, so I send the invoice to the accounting person/department, who need to do something about it, but need to send a copy of the invoice to the manager/owner so they are informed.

Maybe having a CC check box in the contacts section or something would be cool.

But really, To vs CC is minutia when compared to getting every thing bundled into a single mail...

Re: email single invoice to multiple recipients

Fair enough. But then you'd have a problem if there were ONLY "CC" recipients...

Fortunately, in a small business it should be obvious to those concerned why they got the message. And it's quite clear who else got it, so they can always work it out between themselves if necessary.

24 (edited by apmuthu 02/08/2015 05:24:52 pm)

Re: email single invoice to multiple recipients

Whilst getting all contacts and using it for emailing (To, Cc, Bcc, etc) is fine, getting only one contact is important for report printing and for use in the FA user interface screens.

Currently, the function get_branch_contacts is used in:

sales/includes/ui/sales_order_ui.inc
repXXX.php
where XXX in 103 (commented out), 107 (Invoice), 108 (commented out), 109  through to 113.

In the extensions, rep_email_customers calls it in modules/reporting/rep_email_customers/includes/customer_emailer.inc with the last parameter as false (no defaults). Also, when the second parameter ($action) is set to null as in this file, all contacts accrue.

In sales/includes/ui/sales_order_ui.inc the following construct in lines 112-114:

    $contact = get_branch_contacts($branch_id, 'order', $customer_id);
    $order->set_branch($branch_id, $myrow["tax_group_id"],
    $myrow["tax_group_name"], @$contact["phone"], @$contact["email"]);

expect to have defaults (missing last parameter assumed $default as true) and restricted to the 'orders' type alone and is used as a single contact for populating the branch details alone.

The standard repXXX.php listed above use the following constructs:

rep107.php:
$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);

rep109.php:
$contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);

rep110.php:
$contacts = get_branch_contacts($branch['branch_code'], 'delivery', $branch['debtor_no'], true);

rep111.php:
$contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);

rep112.php:
$contacts = get_branch_contacts($myrow['branch_code'], 'invoice', $myrow['debtor_no']);

rep113.php:
$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);

which makes last parameter $default to be true but allows for (but currently does not obtain) multiple contacts in subsequent emailing usage alone.

Either we determine it to be a single contact (count elements in array), or choose only the first one for the sales/includes/ui/sales_order_ui.inc and all other places we need single contacts or make a new function called get_branch_contact (singular) with the current code in it and make new content for the existing function get_branch_contacts (plural) for usage where multiple contacts are needed.

As far as emailing is concerned, we can make the first contact as the "To" recipient and all others as the "Cc" recipients if they exist!

Way forward is to be decided - all your thoughts on this is welcome before a patch is forwarded to @joe.

Hence currently, no code change is required except for setting the last parameter as false in reports that need to be emailed to multiple entities!

Re: email single invoice to multiple recipients

It's probably cleaner to have separate functions for single and multiple contacts. In particular, it will establish which contact is the "primary" one and future developers won't have to guess.