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 应用程序错误 发生网站错误。对暂时的不便深表歉意。

但是,如果我用$cleanEmail代码替换'x@y.z'!我应该怎么办?我知道通过使用 gmail 我无法更改发件人姓名,但我想将此代码上传到虚拟主机中,并且我不想在那里遇到此问题。

任何人都可以建议在 Slim 3 中重定向的更好方法吗?而不是这两行:

$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

1

You probably want to do the following instead.

$cleanEmail = filter_var($email,FILTER_SANITIZE_EMAIL);
$cleanMsg   = filter_var($msg,FILTER_SANITIZE_STRING);
于 2016-01-31T11:44:50.690 回答