Skip to content

Latest commit

 

History

History
95 lines (93 loc) · 2.69 KB

File metadata and controls

95 lines (93 loc) · 2.69 KB

email()

Sends an email.

email($email='', $subject='', $content = ''):bool

  • $email (string|array) - email or an array of emails.
  • $subject (string|array) - email subject or an array of email options. Magic.
  • $content (string) - email body.

If no arguments are passed the function returns an object of the special mailer class which allows to use chains of methods.

Simple using

email('[email protected]', 'Subject', 'Message body');
# Sending to multiple users
email(['[email protected]','[email protected]','[email protected]'], 'Subject', 'Message body');
# Use parameters
$params = array(
    // Required
    'subject' => 'Тема',
    'content' => 'Содержание письма',
    // Optional
    'sender'   => '[email protected]',
    'from'     => 'Administrator',
    'fromName' => 'The Greatest MODX society',
    // 'cc' => '',
    // 'bcc' => '',
    // 'replyTo' => '',
    // 'attach' => '',
);
if (! email('[email protected]', $params)) {
    // Some trouble. See the MODX error log.
}

Use the mailer object

email()
	->to('[email protected]')
	->toUser(5)
	->cc('[email protected]') 
	->subject('Subject')
	->content('Message body')
	->tpl('chunkName or file', $params) // ignored if the content is set.
	->from('Administrator')
	->replyTo('[email protected]')
	->attach('path/to/file1.jpg')
	->attach('path/to/file2.png')
	->send();

Queues

Use queues to defer the sending emails.

# Use the mailer object
email()
	->to('[email protected]')
	->toUser(5)
	->cc('[email protected]') 
	->subject('Subject')
	->content('Message body')
	->from('Administrator')
	->replyTo('[email protected]')
	->attach('path/to/file1.jpg')
	->attach('path/to/file2.png')
	->queue(); // or save()

You can create different queues for different tasks.

// Notify managers about a new order.
email()...->save('for_manager');
// Inform the admin about some user action (add to the queue "for_admin"). 
email()...->save('for_admin'); // Set the second parameters to true to rewrite the queue.

All email will be stored to the cache. Then set the cron job.

// email_admin.php
# Connect to MODX
...
# Send emails from the queue (for example, every hour).
email()->sendFromQueue('for_admin');
// or
email()->saved('for_admin');

Testing the email functionality. Use the log() method to save the email data to the log or use the toArray() method to output the data to the page.

# Email to log
email()
	->to('[email protected]')
	->toUser(5)
	->cc('[email protected]') 
	->subject('Subject')
	->tpl(MODX_CORE_PATH . 'chunks/myChunk.tpl', $params)
	->from('Administrator')
	->replyTo('[email protected]')
	->attach('path/to/file1.jpg')
	->attach('path/to/file2.png')
	->log() // to get the email data in the json format use log(true)