Skip to content

Instantly share code, notes, and snippets.

@melz
Forked from lucasmezencio/app.php
Created November 15, 2013 22:03
Show Gist options
  • Save melz/7492424 to your computer and use it in GitHub Desktop.
Save melz/7492424 to your computer and use it in GitHub Desktop.
Swiftmailer + Twig + Silex app.
<?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@example.com'))
->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;
<?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;
[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
<?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