How to email an invoice using your php sdk?

Hi,
I’m using your PHP API for some time already and it’s great so far. However, now I want to email the invoice directly after it’s been created. Is there any way to do it with your php sdk like:

$invoice->save();
$invoice->sendEmail();

Not currently, you’d need to use the API.

http://docs.invoiceninja.com/en/latest/api.html#emailing-invoices

I tried adding this to the sdk however, there’s always an error:

public function sendMail()
    {
		$url = static::getRoute();
		$url = str_replace('invoices', 'email_invoice', $url);

		$invoiceId->id = $this->id;
		$data = static::sendRequest($url, $invoiceId, 'POST');

		return static::hydrate($data, $this);
    }

Error:

[28-Dec-2017 12:50:24 UTC] PHP Warning:  Creating default object from empty value in /home2/{user}/public_html/ninja/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/Invoice.php on line 42
[28-Dec-2017 12:50:24 UTC] PHP Fatal error:  Uncaught Exception: {
    "error": "Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
} in /home2/{user}/public_html/ninja/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php:186
Stack trace:
#0 /home2/{user}/public_html/ninja/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/Invoice.php(43): InvoiceNinja\Models\AbstractModel::sendRequest('https://billing.e...', '{"id":18}', 'POST')
#1 /home2/{user}/public_html/ninja/createClient.php(37): InvoiceNinja\Models\Invoice->sendMail()
#2 {main}
  thrown in /home2/{user}/public_html/ninja/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php on line 186

Any idea what might be wrong?

Is it working if you call the API directly?

Ya, running
curl -X POST https://billing.<domain>.de/api/v1/email_invoice -d '{"id":18}' -H "Content-Type:application/json" -H "X-Ninja-Token: <Token>"

from commandline works.

Are you testing with the same invoice id?

I’d suggest echoing out the URL request and comparing it.

Ya, I’m using the same invoice id.

The response from the command line curl is:

{
    "message": "success"
}

…as expected.

Sorry, not sure…

I suppose something is wrong with passing the invoice id. I didn’t really get how to pass the data ($data in Abstract class).

$data = ['id' => value];

Absolutely. So did I. Maybe a new installation will solve this. I’ll update you soon.

Ok, I resolved the first error by using “localhost” instead of the full domain and setting mail from smtp to sendmail (but I’m not sure if the last one actually caused the issue, maybe just a timeout). Now the email is send and I get a “successful” message, however along with another error:
Fatal error: Uncaught Exception: { “message”: “success” } in /home/<user>/public_html/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php:186
Should I take this a raw output? Actually I want to check the value of $invoice->message to see if it was successful. Any ideas on how to do this?

Btw: How should I share final code so you can add it to sdk?

One option would be to add a check here for { "message": "success" } response:

https://github.com/invoiceninja/sdk-php/blob/master/src/InvoiceNinja/Models/AbstractModel.php#L186

You can create a pull request against the SDK repo however I’m not sure it would get merged as we’d likely take a different approach with the implementation.

Well, setting raw to true did the trick. It’s working fine now. :slight_smile:
Thanks for your help.

Great to hear!