IP 2.0 - email not working

While sending email, nothing happens. In browser developer console shows ‘500 Internal Server Error’ in …/invoice_mail/store.

The version is still in alpha so please do not expect specific help for such issues.

Also: please post logs about this error. Error 500 is not helpful at all.

Please check your settings. Email is working fine on my end using SMTP. If you can post the log, I may be able to help.

I have crosschecked the SMTP settings, there is no issue.
Here is log, I found in log file:
27.7.226.86 - - [13/Jun/2018:07:06:11 -0400] “POST /invoice_mail/store HTTP/1.1” 500 33 “http://bill.domail.com/invoices?status=all_statuses” “Mozilla/5.0 (Windows NT 6.2; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0”

Sorry, the log file can be found here, notice the file name has the date in it.

/storage/logs/laravel-2018-06-14.log

I did however notice that all of the fields must be filled when sending an email including the CC and BCC fieilds on the form. When they were not filled, it wouldn’t send and showed no error.

Try filling in all fields and sending the invoice. I have a work around for that but not necessarily a fix.

1 Like

Thanks a lot, you are right all fields are required to be filled. I think it’s a bug we should consider this in beta release.
Could you please suggest any workaround to fix this issue?
Thanks again, I really appreciate your help.

A work around would be to remove the fields that aren’t being used. For example, I didn’t need the CC but wanted to keep the BCC. Below, I removed both CC and BCC. Unless you remove these fields from the view files, you will still see the fields, you just won’t be able to use them.

Edit this file:
/app/Modules/MailQueue/Support/MailQueue.php

<?php

/**
 * InvoicePlane
 *
 * @package     InvoicePlane
 * @author      InvoicePlane Developers & Contributors
 * @copyright   Copyright (C) 2014 - 2018 InvoicePlane
 * @license     https://invoiceplane.com/license
 * @link        https://invoiceplane.com
 *
 * Based on FusionInvoice by Jesse Terry (FusionInvoice, LLC)
 */

namespace FI\Modules\MailQueue\Support;

use FI\Support\PDF\PDFFactory;
use Illuminate\Support\Facades\Mail;

class MailQueue
{
    protected $error;

    public function create($object, $input)
    {
        return $object->mailQueue()->create([
            'from' => json_encode(['email' => $object->user->email, 'name' => $object->companyProfile->company]),
            'to' => json_encode($input['to']),
            'subject' => $input['subject'],
            'body' => $input['body'],
            'attach_pdf' => $input['attach_pdf'],
        ]);
    }

    public function send($id)
    {
        $mail = \FI\Modules\MailQueue\Models\MailQueue::find($id);

        if ($this->sendMail(
            $mail->from,
            $mail->to,
            $mail->subject,
            $mail->body,
            $this->getAttachmentPath($mail)
        )
        ) {
            $mail->sent = 1;
            $mail->save();

            return true;
        }

        return false;
    }

    private function sendMail($from, $to, $subject, $body, $attachmentPath = null)
    {
        try {
            $htmlTemplate = (view()->exists('email_templates.html')) ? 'email_templates.html' : 'templates.emails.html';

            Mail::send([$htmlTemplate, 'templates.emails.text'], ['body' => $body], function ($message) use ($from, $to, $subject, $attachmentPath) {
                $from = json_decode($from, true);
                $to = json_decode($to, true);
                $message->from($from['email'], $from['name']);
                $message->subject($subject);

                foreach ($to as $toRecipient) {
                    $message->to(trim($toRecipient));
                }

                if (config('fi.mailReplyToAddress')) {
                    $message->replyTo(config('fi.mailReplyToAddress'));
                }

                if ($attachmentPath) {
                    $message->attach($attachmentPath);
                }
            });

            if ($attachmentPath and file_exists($attachmentPath)) {
                unlink($attachmentPath);
            }

            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();

            return false;
        }
    }

    private function getAttachmentPath($mail)
    {
        if ($mail->attach_pdf) {
            $object = $mail->mailable;

            $pdfPath = base_path('storage/' . $object->pdf_filename);

            $pdf = PDFFactory::create();

            $pdf->save($object->html, $pdfPath);

            return $pdfPath;
        }

        return null;
    }

    public function getError()
    {
        return $this->error;
    }
}
1 Like

Many thanks for your kind help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.