A
A
Andrey Shakhtarin2016-10-25 16:42:40
symfony
Andrey Shakhtarin, 2016-10-25 16:42:40

How to properly send messages via swiftmailer Symfony2-3?

Good day to all! Faced a new task: sending messages via symfony2-3:
problem with authenticate on the gmail side.
What I have in stock:

//app/config/config.yml
//настройки swiftmailer для отправки сообщения на почту:
swiftmailer:
    transport: stmt
    username: [email protected]
    password: password
    host: smtp.gmail.com
    port: 465
    encryption: ssl
    auth_mode:  login

to connect and redirect the letter, you need to connect the pop-client.
POP client instructions:

Step 1: Make Sure You Need POP
You can use IMAP or POP to work with Gmail in third-party clients.
Using the IMAP protocol, you can work with Gmail mail on several devices at once. Letters in this case are synchronized in real time.
POP access can only be enabled on one computer. In this case, letters will not be synchronized in real time - they will be downloaded to the client at the frequency that you specify.
Step 2: Set up POP access Set
up POP in Gmail first
Open Gmail on your computer.
In the upper right corner, click on the Settings icon.
Click Settings.
Click the Forwarding and POP/IMAP tab.
In the "POP Access" section, select Enable POP for all emails or Enable POP for emails received since now.
Click Save Changes at the bottom of the page.
Then configure your mail client
Open a client (for example, Microsoft Outlook) and set the following parameters:
Incoming mail server (POP)
pop.gmail.com
SSL required: yes
Port: 995
Outgoing mail server (SMTP)
smtp.gmail.com
SSL required: Yes
TLS Required: Yes (if available)
Authentication Required: Yes
SSL Port: 465
TLS/STARTTLS Port: 587
If you use Gmail at your organization or school, enter mail.domain.com and select port 110.
Server timeout More than 1 minute (5 minutes recommended)
Full name or display name Your name
Account, username or email address Your email address
Password Your Gmail password

Necessary settings for sending a message from the project:

  1. symphony parameter ("gearbox" (relay) by ports) - transport: smtp
  2. Outgoing mail server (SMTP) - host: smtp.gmail.com
  3. Requires SSL: yes - encryption: ssl
  4. Authentication required: yes - auth_mode: login
  5. Port for SSL: 465 - port: 465
  6. Account, username or email - username: [email protected]
  7. Your Gmail password - password: password

Total result:


swiftmailer:
    transport: stmt
    username: [email protected]
    password: password
    host: smtp.gmail.com
    port: 465
    encryption: ssl
    auth_mode:  login



Now it's up to you to send a message:
class SendMailController extend Controller
{
    //до сюды даже не доходит 
      $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
                $this->renderView(
                    'AppJoboardBundle:Affiliate:email.txt.twig',
                    array('name' => 'mailer')
                )
            )
        ;
        $this->get('mailer')->send($message);
}

When sending a message, this is what happens:

Failed to authenticate on SMTP server with username "[email protected]" using 1 possible authenticators

From this it is clear: Failed to authenticate this user on the SMTP server with the username (login) "[email protected]" using 1 possible authenticator. (perhaps the problem here is transport: smtp , although I deeply doubt it).
As I understand it, all the parameters are indicated and everything was done correctly, but I missed something. Please tell me what did you do wrong? How can and should I send messages from a project through symfony?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Shakhtarin, 2016-10-25
@AndreyShakhtarin

After a short search, I found a solution:
In principle, everything was done correctly, with the exception of one nuance.
I will describe a detailed description of how I did everything, maybe it will help someone, although everything is in the docks: well, or almost everything ... "he-he-he" ....
the first thing I did was add parameters for swiftmailer:

//app/config/config.yml
...
# Swiftmailer Configuration
swiftmailer:
    transport: smtp
    username: "%mailer_user%"
    password: "%mailer_password%"
    host: "%mailer_host%"
    port: "%mailer_port%"
    encryption: "%mailer_encryption%"
    auth_mode:  "%mailer_auth_mode%"

Data in the parameters in quotes, for example "%mailer_auth_mode%" as a referencing parameter to the file with parameters that I import in the same place in the configs above:
//app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: "@AppJoboardBundle/Resources/config/parameter_mailer.yml" }

Next, I created a file in my bundle as indicated by the above AppJoboardBundle/Resources/config/parameter_mailer.yml
(Maybe this is not the best practice, but I got tired of constantly adding parameters after updating the project, so I will add them to my bundle and import them in configs. )
and add parameters:
parameters:
    mailer_user:  [email protected]
    mailer_password:  password
    mailer_host: smtp.gmail.com
    mailer_port: 465
    mailer_encryption: ssl
    mailer_auth_mode:  login

Then I CREATE A NEW ACCOUNT in gmail.com (To be honest, this was my problem, I don’t know why, but letters were not sent from the accounts that I had previously created, no matter what I did there ... Maybe I didn’t do much :-) )
After I created an account, I connected the pop-client:
(described by me earlier, I repeat once again):
Open Gmail on your computer.
In the upper right corner, click on the Settings icon.
Click Settings.
Click the Forwarding and POP/IMAP tab.
In the "POP Access" section, select Enable POP for all emails or Enable POP for emails received since now.
Click Save Changes at the bottom of the page.
And in the end, the last step remains to describe the sending in the controller:
(well, il where you think is fair)))
$message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
                $this->renderView(
                    'AppJoboardBundle:Affiliate:email.txt.twig',
                    array('name' => 'mailer')
                )
            )
        ;
        $this->get('mailer')->send($message);

And that's all you need to do in order to send emails through swiftmailer, redirecting to the newly created mail and sending emails to the specified addresses by users in your project.

M
mind3, 2016-10-28
@mind3

Recently I ran into a similar problem specifically when sending via gmail, it turned out that you need to provide access in the gmail settings for an unknown application, in this case your symfony project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question