Walther Posted October 6 Share Posted October 6 I need to send an email message to my customer and, in cc, to a couple of different foreign email addresses. But it seems to be impossible... The API SendEmail command ( https://developers.whmcs.com/api-reference/sendemail ) allows you to specify just the user ID, and send te message to the email address of that user ID... but does not allow tospecify any CC or CCN address (note. this is possible when you send a message from the WHMCS admin web interface, so it's not a limit of the system but just of the API function). I tried using the PreEmailSendReduceRecipients hook, but it allows just to remove a predefined CC recipient, not to add a new one or redefiny an existing one... Any ideaabout how to do it? 0 Quote Link to comment Share on other sites More sharing options...
Waqas Ahmed Waseer Posted October 28 Share Posted October 28 Ran into the same wall when we wanted a client's accountant copied on billing mails. You're right about the hooks: EmailPreSend can only add merge fields, attach files or abort the send, and PreEmailSendReduceRecipients can only remove CC/BCC entries, never add them. So it has to come from the recipient side. Three ways, depending on how fixed the extra addresses are: 1. Same extra addresses every time: create a custom email template (General type) under Configuration > System Settings > Email Templates and fill in its CC and BCC fields. Then call SendEmail with messagename set to that template and id set to the client ID. Every send of that template copies those addresses. 2. Addresses that belong to one client (their accountant, a partner, a second mailbox): add them as Contacts on that client and enable the matching email preferences (General, Invoice, Support and so on). WHMCS copies contacts on emails of the types they're opted into, and that's exactly the cc list PreEmailSendReduceRecipients receives. It also works for a custom message via SendEmail with customtype=general. 3. Different addresses on every send: from your script call AddContact with email_preferences[general]=1, then SendEmail, then DeleteContact. A bit hacky, but the message still lands in the client's email log. If you don't need it logged in WHMCS at all, send that one through your own SMTP (PHPMailer etc.) outside the API. Whichever you pick, test it on a dummy client first and check the Email Log to confirm the CC line looks the way you expect. 0 Quote Link to comment Share on other sites More sharing options...
charlotteclark Posted October 30 Share Posted October 30 Option 2 makes sense for a recurring contact like an accountant, but I'd double-check the opt-in scopes: if the client's contacts are only opted into Invoices, a General-type SendEmail call won't copy them, so the preference has to match the customtype you pass. Option 3 is clever but I'd worry about churn — AddContact/DeleteContact on every send could clutter the client's contact history and any failure between the calls leaves a stray contact behind. Curious whether anyone has confirmed the CC line actually renders in the Email Log for the template route (option 1), or if it only shows the primary recipient. 0 Quote Link to comment Share on other sites More sharing options...
Huzaifa Azeem Posted October 31 Share Posted October 31 Have you considered updating the CC field of the email template directly in the database before calling the SendEmail API? Since WHMCS already stores the CC recipients with the email template, you could potentially update that field with the required email addresses before sending the email. The SendEmail API would then use the updated template configuration. However, this approach would need testing, especially if multiple emails are sent simultaneously using the same template, as it could cause recipient conflicts. For fixed CC addresses, configuring them directly in the email template should be sufficient. For dynamic CC addresses, a database-based approach might be worth exploring. 0 Quote Link to comment Share on other sites More sharing options...
Waqas Ahmed Waseer Posted 7 hours ago Share Posted 7 hours ago @charlotteclark good question, and I can answer part of it from the docs. The developer reference for the EmailPreLog hook, which runs just before an email is written to the log, lists the recipient, cc and bcc as separate values of the entry. So the CC and BCC addresses are recorded with the message, not only the primary recipient. The docs don't say how the Email Message Log screen lays that out, so I can't promise the exact wording of the CC line you'll see. The quickest way to settle it is a test. Make a custom General email template with one CC address filled in, send it through SendEmail to a dummy client, then open that message from the client's Emails tab and from Configuration, System Logs, Email Message Log. And check the headers of the copy that lands in the CC mailbox, since that is what proves the CC was actually applied. You're right about the opt-in scopes for contacts, by the way. The docs say a contact receives copies of General messages only if they have the General notification option on, product messages only with the Product option, and domain messages only with the Domain option. So the contact's preference has to match the type of message you send. On the database edit idea, I'd avoid it. Rewriting the template's CC field before each send is racy, as the suggestion itself admits, and it changes the template for every other email using it. For addresses that vary per client, contacts with the right preferences do the job without touching template rows. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.