Swift Mailer官方摘要

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:


$ composer require swiftmailer/swiftmailer

Create and send Message

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('[email protected]' => 'John Doe')) 
// Set the To addresses with an associative array 
  ->setTo(array('[email protected]', '[email protected]' => 'A name')) 
// Give it a body 
  ->setBody('Here is the message itself') 
// And optionally an alternative body 
  ->addPart('Here is the message itself', 'text/html') 
// Optionally add any attachments 
  ->attach(Swift_Attachment::fromPath('my-document.pdf')) ;

// Send the message by mailer
$result = $mailer->send($message);

下面的可以不看


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



require_once '/path/to/swift-mailer/lib/swift_required.php';


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, ...):

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');

你可能感兴趣的:(Swift Mailer官方摘要)