0

swiftmailer と gmail を使用して、連絡先フォームのメールをローカルに送信しようとしています。私はすべての行をチェックしましたが、問題は 'setFrom' であることを知っています。

$app->post('/contact', function ($request, $response, $args){
$body = $this->request->getParsedBody();

$name = $body['name'];
$email = $body['email'];
$msg = $body['msg'];

if(!empty($name) && !empty($email) && !empty($msg) ){
    $cleanName  = filter_var($name,FILTER_SANITIZE_STRING);
    $cleanEmail = filter_var($name,FILTER_SANITIZE_EMAIL);
    $cleanMsg   = filter_var($name,FILTER_SANITIZE_STRING);

}else {
    //redirecting to contact page
}

//sending email

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
    ->setUsername('xxx@gmail.com')
    ->setPassword('xxx');

$mailer= \Swift_Mailer::newInstance($transporter);

$message = \Swift_Message::newInstance();

$message->setSubject('Email from our website');
$message->setTo(array('ns.falahian@gmail.com'));
$message->setBody($cleanMsg);
$message->setFrom([
    $cleanEmail => $cleanName
]);

$result=$mailer->send($message);

    if ($result > 0) {
        $path = $this->get('router')->pathFor('home');
        return $response->withRedirect($path);
    } else {
        $path = $this->get('router')->pathFor('contact');
        return $response->withRedirect($path);
    }

});

ご覧のとおり、Slim 3 フレームワークも使用しています。コードを実行すると、次のエラーが表示されます。

Slim Application Error ウェブサイトのエラーが発生しました。一時的なご不便をおかけして申し訳ありません。

しかし、 をコードに置き換えると動作します$cleanEmail! 'x@y.z'私は何をすべきか?gmail を使用して送信者名を変更できないことはわかっていますが、このコードを Web ホストにアップロードしたいので、この問題をそこで発生させたくありません。

また、Slim 3 でリダイレクトするためのより良い方法を提案できる人はいますか? これらの 2 行の代わりに:

$path = $this->get('router')->pathFor('contact');
        return $response->withRedirect($path);

ルートの名前を次のように設定しました。

$app->get('/contact', function ($req, $res, $args) {
     return $this->view->render($res, "contact.twig");
})->setName('contact');

どうもありがとう!

4

1 に答える 1