Email class methods

forge($setup = null, array $config = array())

The forge returns a new Email_Driver instance based on the config or input it receives.

Static Yes
Parameters
Param Type Default Description
$setup mixed
null
Supply null for default config, a setup group name, or a config array.
$config mixed
null
An additional config array to modify default configs on the fly.
Returns a new Email_Driver instance
Example
// Plain an simple:
$email = \Email::forge();

// Config loaded from a group
$email = \Email::forge('my_defaults');

// Config supplied
$email = \Email::forge(array(
	'driver' => 'smtp',
));

// Loaded config from group with an dynamic overwrite
$email = \Email::forge('my_defaults', array(
	'driver' => 'smtp',
));

Email driver methods

Every driver that extends Email_Driver has these methods. They are all you need to get up and running.

body($body)

The body converts the input to string and sets the message body.

Static No
Parameters
Param Type Default Description
$body string required The message body.
Returns $this
Example
$email->body('This is my message.');

//or pass it a View
$email->body(\View::forge('my/view', $data);

alt_body($alt_body)

The alt_body converts the input to string and sets the message alternative body.

Static No
Parameters
Param Type Default Description
$alt_body string required The message body.
Returns $this
Example
$email->alt_body('This is my alternative message.');

//or pass it a View
$email->alt_body(\View::forge('my/alt/view', $data);

priority($priority)

The priority method sets the mail's priority.

Static No
Parameters
Param Type Default Description
$priority string required Priority: See all email priorities.
Returns $this
Example
$email->priority(\Email::P_HIGHEST);

html_body($html, $generate_alt = null, $auto_attach = null)

The html_body method sets the message body and optionally generates the alt body from it. If specified the inline images will be attached inline automatically.

Please note: By default automatic attaching is on (via config). To turn this off, either supply false, or change the config setting.

Static No
Parameters
Param Type Default Description
$html string required The mail html.
$generate_alt bool
null
Boolean whether to generate an alternative message body, falls back to config default when null is supplied.
$auto_attach bool
null
Set to true or false to attach embedded images, falls back to config default when null is supplied.
Returns $this
Example
$email->html_body(\View::forge('welcome/email', $data));
// Don't generate the alt body
$email->html_body(\View::forge('welcome/email', $data), false);

// Do generate the alt body, but don't auto attach images.
$email->html_body(\View::forge('welcome/email', $data), true, false);

from($email, $name = false)

The from method sets from address and name.

Static No
Parameters
Param Type Default Description
$email string required The from email.
$name string
false
The from name.
Returns $this
Example
$email->from('me@example.com', 'My Name');

subject($subject)

The subject method sets the subject.

Static No
Parameters
Param Type Default Description
$subject string required The email subject.
Returns $this
Example
$email->subject('This is my subject');

to($email, $name = false)

The to method adds an address or an array of addresses to the to recipient array.

Static No
Parameters
Param Type Default Description
$email string|array required The from email.
$name string
false
The from name, ignored when $email is an array.
Returns $this
Example
// Add a single address
$email->to('me@example.com', 'My Name');

// Add multiple addresses
$email->to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

cc($email, $name = false)

The cc method adds an address or an array of addresses to the cc recipient array.

Static No
Parameters
Param Type Default Description
$email string|array required The from email.
$name string
false
The from name, ignored when $email is an array.
Returns $this
Example
// Add a single address
$email->cc('me@example.com', 'My Name');

// Add multiple addresses
$email->cc(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

bcc($email, $name = false)

The bcc method adds an address or an array of addresses to the bcc recipient array.

Static No
Parameters
Param Type Default Description
$email string|array required The from email.
$name string
false
The from name, ignored when $email is an array.
Returns $this
Example
// Add a single address
$email->bcc('me@example.com', 'My Name');

// Add multiple addresses
$email->bcc(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

reply_to($email, $name = false)

The reply_to method adds an address or an array of addresses to the reply to recipient array.

Static No
Parameters
Param Type Default Description
$email string|array required The from email.
$name string
false
The from name, ignored when $email is an array.
Returns $this
Example
// Add a single address
$email->reply_to('me@example.com', 'My Name');

// Add multiple addresses
$email->reply_to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

header($header, $value = null)

The header method adds a custom header to you mail headers.

Static No
Parameters
Param Type Default Description
$header string|array required The header type or array of headers.
$value string
null
The header value.
Returns $this
Example
// Add a single address
$email->header('X-SMTPAP', 'XXXXXXXX');

// Add multiple addresses
$email->reply_to(array(
	'X-SMTPAP' => 'XXXXXX',
	'X-SMTPAP2' => 'XXXXXA',
));

clear_recipients()

The clear_recipients method empties the to, cc and bcc lists.

Static No
Parameters None
Returns $this
Example
// Add a single address
$email->bcc('me@example.com', 'My Name');

// Add multiple addresses
$email->to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_recipients();

clear_addresses()

The clear_addresses method empties the to, cc, bcc and reply to lists.

Static No
Parameters None
Returns $this
Example
// Add a single address
$email->reply_to('me@example.com', 'My Name');

// Add multiple addresses
$email->to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_addresses();

clear_to()

The clear_to method empties the to recipient list.

Static No
Parameters None
Returns $this
Example
// Add multiple addresses
$email->to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_to();

clear_cc()

The clear_cc method empties the cc recipient list.

Static No
Parameters None
Returns $this
Example
// Add multiple addresses
$email->cc(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_cc();

clear_bcc()

The clear_bcc method empties the bcc recipient list.

Static No
Parameters None
Returns $this
Example
// Add multiple addresses
$email->bcc(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_bcc();

clear_reply_to()

The clear_reply_to method empties the reply to list.

Static No
Parameters None
Returns $this
Example
// Add multiple addresses
$email->reply_to(array(
	'me@example.com',
	// with a name
	'me@example.com' => 'His/Her name.',
));

// Reset them
$email->clear_reply_to();

attach($file, $inline = false, $cid = null, $mime = null, $name = null)

The attach method attaches a file.

This method will search for the file in the attachment paths set (config/email.php) in the attach_paths array

Static No
Parameters
Param Type Default Description
$file string
false
Path to the file to include.
$inline bool
false
Whether to attach the file inline.
$cid string
null
The content identifier. Used when attaching inline images.
$mime string
null
By default the mimetype is looked up in the core/config/mimes array, use this to overwrite it.
$name string
null
Attachment name overwrite.
Returns $this
Example
// Add an attachment
$email->attach(DOCROOT.'attachments/report.pdf');

// Attach an image inline
$email->attach(DOCROOT.'assets/img/mail/header.png', true, 'cid:headerimage');
// This image would be bound to <img src="cid:headerimage" />

string_attach($contents, $filename, $cid = null, $inline = false, $mime = null)

The string_attach method attaches a file from a string input.

Static No
Parameters
Param Type Default Description
$contents string required The attachment contents.
$filename string required The filename to use.
$cid string
null
The content identifier. Used when attaching inline images.
$inline bool
false
Whether to attach the file inline.
$mime string
null
By default the mimetype is looked up in the core/config/mimes array, use this to overwrite it.
Returns $this
Example
// Add an attachment
$email->string_attach('This is a textfile', 'test.txt');

clear_attachments()

The clear_attachments method clears the attachments array.

Static No
Parameters None
Returns $this
Example
// Add some attachments
$email->string_attach('This is a textfile', 'test.txt');
$email->attach(DOCROOT.'uploads/attach.pdf');

// And empty the attachments
$email->clear_attachments();

send($validate = null)

The send method sends the mail.

Static No
Parameters
Param Type Default Description
$validate bool
null
Whether to validate the addresses, falls back to config setting.
Returns boolean
Example
try{
	$email->send();
}
catch(\EmailSendingFailedException $e)
{
	// The driver could not send the mail.
}
catch(\EmailValidationFailedException $e)
{
	// One or more email addresses failed validation.
}

get_invalid_addresses()

The get_invalid_addresses method returns the email addresses that did not pass validation.

Static No
Parameters None
Returns $this
Example
try{
	$email->send();
}
catch(\EmailSendingFailedException $e)
{
	// The driver could not send the mail.
}
catch(\EmailValidationFailedException $e)
{
	// One or more email addresses failed validation.
	$these_failed = $email->get_invalid_addresses();
}

return_path($email)

The return_path method sets return-path address.

Static No
Parameters
Param Type Default Description
$email string required The return-path email.
Returns $this
Example
$email->return_path('bounces@example.com');

pipelining($pipelining = true)

The pipelining method enables pipelining (sending multiple messages in one go). For drivers that do not support this, this method is a NOOP. Pipelining is disabled by default.

Static No
Parameters
Param Type Default Description
$pipelining boolean
true
Whether or not pipelining should be enabled.
Returns $this
Example
$email->pipelining(true);

Email getters

get_from()

The get_from method returns the configured "from" address.

Static No
Parameters None
Returns string
Example
// returns the address set by from()
$from = $email->get_from();

get_to()

The get_to method returns the configured "to" addresses.

Static No
Parameters None
Returns array
Example
// returns the addresses set by to()
$to = $email->get_to();

get_cc()

The get_cc method returns the configured "cc" addresses.

Static No
Parameters None
Returns array
Example
// returns the address set by cc()
$cc = $email->get_cc();

get_bcc()

The get_bcc method returns the configured "bcc" addresses.

Static No
Parameters None
Returns array
Example
// returns the address set by bcc()
$bcc = $email->get_bcc();

get_reply_to()

The get_reply_to method returns the configured "reply_to" address.

Static No
Parameters None
Returns string
Example
// returns the addresses set by reply_to()
$reply_to = $email->get_reply_to();

get_body()

The get_body method returns the email body set.

Static No
Parameters None
Returns string
Example
// returns the email body
$text = $email->get_body();

get_subject()

The get_subject method returns the configured email "subject".

Static No
Parameters None
Returns string
Example
// returns the subject set by subject()
$subject = $email->get_subject();