E
E
EVOSandru62018-08-10 14:40:54
Laravel
EVOSandru6, 2018-08-10 14:40:54

How to properly configure Mailer to send mail to VPS?

Good afternoon,
The course of actions listed below was tested on a local machine and on a VPS hosting with DNS configured . Both here and there without an ssl certificate (only http://)
Firstly, the Laravel configuration , the options that I tried in turn:
1) Mail.ru
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mail.ru
MAIL_PORT=465
[email protected] en
MAIL_PASSWORD=secret_pass
MAIL_ENCRYPTION=tls
2) Gmail
#MAIL_DRIVER=smtp
#MAIL_HOST=smtp.gmail.com
#MAIL_PORT=587
#[email protected]
#MAIL_PASSWORD=secret_pass
#MAIL_ENCRYPTION=ssl
3)Mailtrap
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=secret_login
MAIL_PASSWORD=secret_pass
MAIL_ENCRYPTION=null
Tested like this - uncommented each section one by one and ran the tests. For example:

class MailRuTest extends TestCase
{
    protected $mailer;

    protected $mail_host = 'smtp.mail.ru';
    protected $mail_port = 465;
    protected $mail_username = '{secret_login}@mail.ru';
    protected $mail_password = '{secret_pass}';
    protected $mail_encryption = 'tls';

    protected $msg_from = '[email protected]';
    protected $msg_author = 'author_name';
    protected $msg_subject = 'Message From  to car sharing system - system_name';

    protected $msg_to = '[email protected]';

    public function setUp()
    {
        parent::setUp();
        $this->mailer = $this->app->make(MailerInterface::class);
    }

    public function testSend()
    {
        $this->assertTrue(true);

        $this->mailer->raw('Hi, welcome user! ' . date('Y-m-d H:s') . ' - mail_service_name', function ($message) {
            $message->to($this->msg_to)
                ->from($this->msg_from, $this->msg_author)
                ->subject($this->msg_subject);
        });
    }

    public function testAuth()
    {
        try{
            $transport = new Swift_SmtpTransport($this->mail_host, $this->mail_port, $this->mail_encryption);
            $transport->setUsername($this->mail_username);
            $transport->setPassword($this->mail_password);
            $mailer = new Swift_Mailer($transport);
            $mailer->getTransport()->start();
            // $this->expectOutputString('Good mailtrap auth');
            $this->assertTrue(true);

        } catch (Swift_TransportException $e) {
            fwrite(STDERR, print_r($this->mail_host. ': '.$e->getMessage() . ' - ' . $this->mail_host, TRUE));
            return $e->getMessage();

        } catch (\Exception $e) {

            fwrite(STDERR, print_r($this->mail_host. ': '.$e->getMessage() . ' - ' . $this->mail_host, TRUE));
            return $e->getMessage();
        }
        $this->assertTrue(true);
    }
}

In all three tests, I simply duplicated the parameters from .env in the protected properties . in the files GmailTest.php , MailRuTest.php , MailtrapTest.php an attempt is made to contact the smtp server and send a message. As a result: I don't get errors (for example, if I change passwords, then I honestly get into Exception ), but letters are not sent either. The only thing is, for some reason, in the mailtrap.io account, you can see that there were outgoing ones, but the mail does not even arrive in spam on the recipient's mail. And for tests, this mailtrap is more, as I understand it. For gmail


I tried to do the following:
1) gmail->Settings->Forwarding and POP / IMAP->IMAP == on
2) Activated: Insecure applications are allowed at:
https://myaccount.google.com/lesssecureapps
For mail.ru - there is mail for business
Set up MX records and receive letters:
DKIM signature is configured on the domain {domen}.{com}
Mail for the domain is connected!
The result is the same on local and on VPS . The SSL certificate is not installed anywhere, if that matters. Please advise what am I doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-08-20
@EVOSandru6

It works for me like this:
MAIL_DRIVER=mail
and make sure that $msg_from = '[email protected]'; - an existing box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question