Developer-Guide/phpOMS/services/mail.md

4.5 KiB

Mail

Sending and receiving emails is possible with the included mail classes. The mailing is implemented in such a way that you can create emails which you then can pass to a mail handler which sends the mails.

Sending

For email sending external mail servers are recommended, especially for advertisements, mailing lists etc. since a self hosted mailing server is very likely to get blacklisted by email services.

Mail handler

Handler

Available mail handlers are:

  • IMAP
  • POP3
IMAP Connection
// Define mailer
$handler = new Imap('testuser', 'testuser', 143);
$handler->host  = '127.0.0.1';
$handler->flags = '/imap/notls/norsh/novalidate-cert';

// ...

// Send email
$handler->send($mail);
POP3 Connection
// Define mailer
$handler = new Pop3('testuser', 'testuser', 110);
$handler->host  = '127.0.0.1';
$handler->flags = '/pop3/notls';

// ...

// Send email
$handler->send($mail);

Software

For sending emails you can chose between the following mailing programs depending on your server setup:

  • Sendmail
  • Mail
  • Smtp
// Mail
$handler->setMailer(SubmitType::MAIL);

// SMTP
$handler->setMailer(SubmitType::SMTP);
$handler->useAutoTLS = false;

// Sendmail
$handler->setMailer(SubmitType::SENDMAIL);

Confirmation Address

$mail                      = new Email();
$mail->confirmationAddress = 'test1@jingga.app';

From, To, CC & BCC

$mail = new Email();
$mail->setFrom('test1@jingga.app', 'Dennis Eichhorn');
$mail->addTo('test@jingga.app', 'Dennis Eichhorn');
$mail->addCC('test2@jingga.app', 'Dennis Eichhorn');
$mail->addBCC('test3@jingga.app', 'Dennis Eichhorn');

Reply

$mail = new Email();
$mail->addReplyTo('test4@jingga.app', 'Dennis Eichhorn');

Subject

$mail          = new Email();
$mail->subject = 'Test subject';

Body

Plain

$mail       = new Email();
$mail->body = 'Body';

Html

$mail          = new Email();
$message       = \file_get_contents(__DIR__ . '/files/utf8.html');
$mail->charset = CharsetType::UTF_8;
$mail->body    = '';
$mail->bodyAlt = '';

$mail->setHtml(true);
$mail->msgHTML($message, __DIR__ . '/files');

Calendar

$mail          = new Email();
$mail->body    = 'Ical test';
$mail->altBody = 'Ical test';
$mail->ical    = 'BEGIN:VCALENDAR'
    . "\r\nVERSION:2.0"
    . "\r\nPRODID:-//phpOMS//Karaka Calendar//EN"
    . "CONTENT"
    . "\r\nCALSCALE:GREGORIAN"
    . "\r\nX-MICROSOFT-CALSCALE:GREGORIAN"
    . "\r\nBEGIN:VEVENT"
    . "\r\nUID:201909250755-42825@test"
    . "\r\nDTSTART;20190930T080000Z"
    . "\r\nSEQUENCE:2"
    . "\r\nTRANSP:OPAQUE"
    . "\r\nSTATUS:CONFIRMED"
    . "\r\nDTEND:20190930T084500Z"
    . "\r\nLOCATION:[London] London Eye"
    . "\r\nSUMMARY:Test ICal method"
    . "\r\nATTENDEE;CN=Attendee, Test;ROLE=OPT-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP="
    . "\r\n TRUE:MAILTO:attendee-test@example.com"
    . "\r\nCLASS:PUBLIC"
    . "\r\nDESCRIPTION:Some plain text"
    . "\r\nORGANIZER;CN=\"Example, Test\":MAILTO:test@example.com"
    . "\r\nDTSTAMP:20190925T075546Z"
    . "\r\nCREATED:20190925T075709Z"
    . "\r\nLAST-MODIFIED:20190925T075546Z"
    . "\r\nEND:VEVENT"
    . "\r\nEND:VCALENDAR";

Attachments

External file

$mail = new Email();
$mail->addAttachment(__DIR__ . '/files/logo.png', 'logo');

Inline

$mail = new Email();
$mail->setHtml(true);
$mail->msgHTML("<img alt=\"image\" src=\"cid:cid1\">");
$mail->addEmbeddedImage(__DIR__ . '/files/logo.png', 'cid1');

String files

$mail = new Email();
$mail->addStringAttachment('String content', 'string_content_file.txt');
$mail->addStringEmbeddedImage(\file_get_contents(__DIR__ . '/files/logo.png'), 'cid2');

Receiving

Handler

IMAP Connection

// Define mailer
$handler = new Imap('testuser', 'testuser', 143);
$handler->host  = '127.0.0.1';
$handler->flags = '/imap/notls/norsh/novalidate-cert';

POP3 Connection

// Define mailer
$handler = new Pop3('testuser', 'testuser', 110);
$handler->host  = '127.0.0.1';
$handler->flags = '/pop3/notls';

Mailboxes

List, Create, Rename, Delete

$handler->getBoxes();
$handler->createBox('TestBox');
$handler->renameBox('TestBox', 'NewTestBox');
$handler->deleteBox('NewTestBox');

Info

$handler->getMailboxInfo('INBOX.TestBox');

$handler->countMail('INBOX.TestBox');
$handler->handler->countRecent('INBOX.TestBox');
$handler->handler->countUnseen('INBOX.TestBox');

Mail List

$handler->getHeaders('INBOX')