Introduction
Swift Mailer is a component-based library for sending e-mails from PHP applications.
Install
The preferred way to install Swiftmailer is via Composer:
<pre>
$ composer require swiftmailer/swiftmailer
</pre>
Create and send Message
<pre>
require_once 'lib/swift_required.php';
//get a transport instance
//include smtp server port
//username
//password
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password');
//get a Mailer instance by your config
$mailer = Swift_Mailer::newInstance($transport);
//builde a message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(array('john@doe.com' => 'John Doe'))
// Set the To addresses with an associative array
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf')) ;
// Send the message by mailer
$result = $mailer->send($message);
</pre>
下面的可以不看
Including Swift Mailer (Autoloading)
If you are using Composer, Swift Mailer will be automatically autoloaded.
If not, you can use the built-in autoloader by requiring the swift_required.php
<pre>
<code>
require_once '/path/to/swift-mailer/lib/swift_required.php';
</code>
</pre>
If you want to override the default Swift Mailer configuration, call the init() method on the Swift
class and pass it a valid PHP callable (a PHP function name, a PHP 5.3 anonymous function, ...):
<pre>
require_once '/path/to/swift-mailer/lib/swift_required.php';
function swiftmailer_configurator() {
// configure Swift Mailer
Swift_DependencyContainer::getInstance()->...
Swift_Preferences::getInstance()->...
}
Swift::init('swiftmailer_configurator');
</pre>