Created
October 30, 2012 11:47
-
-
Save lucasmezencio/3979768 to your computer and use it in GitHub Desktop.
Sending emails with Silex Bootstrap (http://github.com/lucasmezencio/silex-bootstrap) and Swiftmailer + Twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
$app = require __DIR__.'/bootstrap.php'; | |
$app->match('/contact/', function(Request $request) use ($app) { | |
if ('POST' == $request->getMethod()) { | |
$data = array( | |
'nome' => $request->get('name'), | |
'email' => $request->get('email') | |
); | |
$message = $app['mailer'] | |
->createMessage() | |
->setSubject('Subject') | |
->setFrom(array($request->get('email'))) | |
->setTo(array('[email protected]')) | |
->setBody($app['twig']->render('email.html.twig', $data), 'text/html'); | |
$app['mailer']->send($message); | |
if ($request->isXmlHttpRequest()) { | |
return $app->json(array( | |
'status' => true | |
)); | |
} | |
} | |
return $app['twig']->render('contact.html.twig'); | |
})->bind('contact'); | |
return $app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../vendor/autoload.php'; | |
use Silex\Provider\TwigServiceProvider; | |
use Silex\Provider\SwiftmailerServiceProvider; | |
$env = getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'; | |
$ini_config = parse_ini_file(__DIR__.'/config.ini', TRUE); | |
$config = $ini_config[$env]; | |
$app = new Silex\Application(); | |
$app->register(new TwigServiceProvider(), array( | |
'twig.path' => __DIR__.'/templates', | |
'twig.options' => array('cache' => __DIR__.'/../cache'), | |
)); | |
$app->register(new SwiftmailerServiceProvider(), array( | |
'swiftmailer.options' => array( | |
'host' => $config['mail.host'] | |
'port' => $config['mail.port'] | |
'username' => $config['mail.username'] | |
'password' => $config['mail.password'] | |
'encryption' => $config['mail.encryption'] | |
'auth_mode' => $config['mail.auth_mode'] | |
) | |
)); | |
return $app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[production] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login | |
[staging] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login | |
[development] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app = require __DIR__.'/../src/app.php'; | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No trecho
você tem dois níveis de identação. Dá pra melhorar.