Arthas
Arthas

Reputation: 51

Symfony ApiPlatform React - CORS

I have a problem... With APIPlatform and React, i want to send a mail with Symfony mailer, i use a custom operations.

here are all of my pages to realize my option

    <?php
namespace App\Controller;

use App\Entity\Contact;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;

class ContactMailController {

    private $mail;

    public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }


    public function __invoke(Contact $data){
        $prenom=htmlspecialchars(strip_tags($data->getPrenom()));
        $nom=htmlspecialchars(strip_tags($data->getNom()));
        $message=htmlspecialchars(strip_tags($data->getMessage()));
        $mail=htmlspecialchars(strip_tags($data->getMail())); 
        $email = (new TemplatedEmail())
            ->from('[email protected]')
            ->to('[email protected]')
            ->subject("contact")
            ->htmlTemplate('email/contact.html.twig')
            ->context([
                'nom'=>$nom,
                'prenom'=>$prenom,
                'mail'=>$mail,
                'message'=>$message
            ]);
            $this->mailer->send($email);
        dd($data);    

    }

}

I connect that to an entity

/**
 * @ApiResource(
 *      itemOperations={},
 *      collectionOperations={"mail"={
 *          "method"="post", 
 *          "path"="/contacts/mailer",
 *          "controller"="App\Controller\ContactMailController",
 *          "openapi_context"={
 *              "summary"="Envoyer un mail à [email protected]",
 *              "description"="Permet d'envoyer un e-mail à [email protected]"
 *          }
 *      }},
 *      denormalizationContext={"disable_type_enforcement"=true}
 *)
 */
class Contact{
...

With Postman, it's done BUT with React

const contactSubmit = async (event)=>{
      ...
                try{
                    axios.post("http://myapi.com/api/contacts/mailer",fields)
                    toast.info('votre message à bien été envoyé')
                    setFields({
                        "prenom": "",
                        "nom": "",
                        "mail": "",
                        "message": ""
                    })
                }catch(error){
                    //
                }  
     ...
    }

I have an error : Access to XMLHttpRequest at 'http://myapi.com/api/contacts/mailer' from origin 'http://mywebsite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

But i have in my .env

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^.*$
###< nelmio/cors-bundle ###

and the worst part is that it is sent, it works but I don't want to see the error displayed ...

Thanks for your help!

Upvotes: 0

Views: 1799

Answers (1)

Arthas
Arthas

Reputation: 51

I solved my problem ! my error was in the Controller, I had not made a response

   ...
    ->context([
                'nom'=>$nom,
                'prenom'=>$prenom,
                'mail'=>$mail,
                'message'=>$message
            ]);
            $this->mailer->send($email);
        // my mistake
        return new Response("sent"); 

Upvotes: 2

Related Questions