Monday, January 27, 2014

how to setup sendgride with symfony 2


Hi,

Today i'm going to help you.How to config mail With SendGrid. :)

SendGrid provides two ways to send email: through our SMTP relay or through our web API.

I recommend to use SMTP.The main reason to use SMTP the large number of libraries and documentation available for that protocol.

let's start..

1 Step

you have to set minimal changes in :parameters.yml in app/config folder

*Change your SMTP username and password to your SendGrid credentials
*Set the server host name to smtp.sendgrid.net
*Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections


parameters:
 
    mailer_transport: smtp
    port: 587
    encryption: ~
    mailer_host: smtp.sendgrid.net
    mailer_user: username in sendgride
    mailer_password: password in sendgride
 

2 Step

You can create a email Controller in own your way.i created a controller like this



<?php

namespace Revo\CrmBundle\Controller;

use FOS\UserBundle\Mailer\Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;;

class EmailController extends Controller
{

    public function getCurrentUser(){
        $userManager = $this->get('fos_user.user_manager');
        $user=$userManager->findUserByUsername($this->getUser());
        return $user;
    }
 
    public function sendMail($mailData){
     
$to = $mailData['to'];
        $subject = $mailData['subject'];
        $message = $mailData['msg'];
        $from = $mailData['from'];
        $headers = "From:" . $from;

        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($from)
            ->setTo($to)
            ->setBody($message);

        return  $this->get('mailer')->send($message);

    }

    public function sendMailToContactsAction($deal_id){

        $mailData['to']="abc@123.com";
        $mailData['subject']="test subject";
        $mailData['msg']="This is a test message";
         $mailData['from']="test@123.com";

        return new Response($this->sendMail($mailData));
    }


}

Done..!!!!


No comments:

Post a Comment