1

Я хочу отправить письмо с локольного сервера используя swiftmailer и gmail на symfony. Вот фрагменты кода

/src/MailerBundle/controller/defaultController

<?php

namespace MailerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $Request = $this->getRequest();
        if($Request->getMethod()=="POST") {
            $name = $Request->get('name');
            $email = $Request->get('email');
            $subject =$Request->get('subject');
            $message = $Request->get('message');
            $message = \Swift_Message::newInstance()
                ->setSubject($subject)
                ->setFrom($email)
                ->setTo('any@whatever.com')
                ->setBody('ExampleBundle:Default:mail.html.twig', 'text/html');

# Send the message
            $this->get('mailer')
                ->send($message);
        }
        return $this->render('MailerBundle:Default:index.html.twig');
    }
}
//config_dev.yml
swiftmailer:
    transport: %dev_mailer_transport%
    host:      %dev_mailer_host%
    username:  %dev_mailer_user%
    password:  %dev_mailer_password%
    spool:     { type: memory }
//parameters.yml

# This file is auto-generated during the composer install
parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: symfony
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: 3rw23ewre343ewrew3434rwrew34343wre
# Mail setup for development environment
dev_mailer_transport: gmail
dev_mailer_host: null
dev_mailer_user: example@gmail.com
dev_mailer_password: password

# Mail setup for production environment
prod_mailer_transport: smtp
prod_mailer_host: localhost
prod_mailer_user: null
prod_mailer_password: null

но как то не получается, перепрововал все методы. Помогите

1 ответ 1

1

Возможно проблема в двухфакторной авторизации у вас на учетной записи gmail. В этом случае придется получить отдельный пароль для приложения и использовать его в параметре mailer_password.

Также книга рецептов symfony рекомендует посмотреть справку по доступам.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.