liuch / dmarc-srg

A php parser, viewer and summary report generator for incoming DMARC reports.
GNU General Public License v3.0
213 stars 31 forks source link

Allow summary reports to be mailed by SMTP #97

Closed dave-slaughter closed 3 months ago

dave-slaughter commented 10 months ago

Adds the ability to use PHPMailer to send reports via SMTP. The default mailing options remains as is.

dstorozhuk commented 3 months ago

Here is what I did in summary_report.php:

composer require rodrigoq/phpmailersendgrid

use Exception;
use PHPMailer\PHPMailer\PHPMailerSendGrid;

And then replaced the mail() call with:

    send_email(
        $emailto,
        mb_encode_mimeheader($subject, 'UTF-8'),
        implode("\r\n", $mbody->content()),
        $headers
    );

SG_KEY constant which I define in conf.php. It is my SendGrid API key.

function send_email(string $emailto, string $subject, string $body, array $headers): void {

    $mail = new PHPMailerSendGrid(true);
    try {
        // Create instance of PHPMailer class
        $mail->isSendGrid();
        $mail->SendGridApiKey = SG_KEY;
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->setFrom('dima@example.com', 'Admin i4w DMARC');
        $mail->addAddress($emailto);

        $mail->AltBody = $body;
        $mail->send();
    }
    catch (Exception $e) {
        echo $e->getMessage();
        echo "Message could not be sent. Mailer Error: " . $mail->ErrorInfo;
    }
}

SendGrid offers free 100 emails per day. Should be enough.

dstorozhuk commented 3 months ago

I would propose to create interface for mailers. And then, if I want to use my mailer - i can create a new class, define in configuration which class to use and simply use my email sender similarly as I did in code example. I will create a PR for that or propose a changes.

liuch commented 3 months ago

@dstorozhuk Right now I'm implementing the ability to specify the type and library for sending emails.

dstorozhuk commented 3 months ago

I am curious if @dave-slaughter still active on this issue. If not - probably, we need to branch from his repo, close that PR and open a new one to complete what @williamdes said about adding the PHPMailer to composer, also add the extendable interface to integrate other email services like sendgrid or postmark or similar.

dstorozhuk commented 3 months ago

@dstorozhuk Right now I'm implementing the ability to specify the type and library for sending emails.

O, that is amazing. I can add the sendgrid integration if you like.

liuch commented 3 months ago

Hello, I have just implemented this feature with 8f76c135f2b5e5d66596c695b4a5fbeb97176a1e in another way (PHPMailer and composer). Thank you for your ideas and code. I'm going to close this PR.