0
我試圖通過swiftmailer和gmail在本地發送一個聯繫表單的郵件。我檢查了每一行,我知道問題是'setFrom'。SwiftMailer的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('[email protected]')
->setPassword('xxx');
$mailer= \Swift_Mailer::newInstance($transporter);
$message = \Swift_Message::newInstance();
$message->setSubject('Email from our website');
$message->setTo(array('[email protected]'));
$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 A website error has occurred. Sorry for the temporary inconvenience.
但如果我'[email protected]'
更換$cleanEmail
代碼工作! 我該怎麼辦? 我知道通過使用Gmail我不能改變發件人的名字,但我想上傳這個代碼在網絡主機,我不想在這個問題。
任何人都可以提出一個更好的方式來重定向在苗條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');
非常感謝!
哇!非常感謝!你救了我!我沒有注意到這個錯誤,我正在遊蕩...... – Niloofar