Override default mailer URL in Symfony

AsMa Boudjemaa :

I am trying to give the possibility to users to send emails to me in Symfony, I could send emails to my self but I am not able to get emails from other emails because the default email adress given in the .env file is not getten override.

  /**
 * @Route("/send_msg", name="send_msg", methods={"GET","POST"})
 */
public function index(Request $request, \Swift_Mailer $mailer)
{

  $name =  $request->query->get('name');
  $mail =  $request->query->get('mail');
  $msg =  $request->query->get('msg');


    $message = (new \Swift_Message('Client'))
        ->setFrom($mail)
        ->setTo('[email protected]')
        ->setBody($msg)

    ;

    $mailer->send($message);
    return new JsonResponse();

}

and this is the .env file

###> symfony/mailer ###
# MAILER_DSN=smtp://localhost
###< symfony/mailer ###

 # For Gmail as a transport, use: "gmail://username:password@localhost"
 # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
 # Delivery is disabled by default via "null://localhost"
  MAILER_URL=gmail://[email protected]:my_pass@localhost
 ###< symfony/swiftmailer-bundle ###

    ###> symfony/sendgrid-mailer ###
    #MAILER_DSN=sendgrid://KEY@default
    ###< symfony/sendgrid-mailer ###
user1026090 :

You're using your Gmail credentials as SMTP server. You can't and shouldn't send from any other email address through your Gmail credentials. Even if you were using another SMTP server that allows any From address, you shouldn't use an email address that's not authenticated in your SMTP service as the From address, because your email would probably never see the light of an inbox.

An option would be to set the Reply-To address to your user's email address by using $message->setReplyTo($mail);. That way if you hit the reply button in your mailbox, the mail should be addressed to the user's email address.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33059&siteId=1