简体   繁体   English

PHP Nette 在有人提交表单时抛出错误

[英]PHP Nette Throws Error When Someone Submits Form

I get this error whenever someone submits the form:每当有人提交表单时,我都会收到此错误:

Fatal error: Uncaught Nette\Mail\SendException: Unable to send email.致命错误:未捕获 Nette\Mail\SendException:无法发送 email。

Here is my code:这是我的代码:

if ($form->isSubmitted()) {
  $values = $form->getValues();

  $mail = new \Nette\Mail\Message;
  $mail->setFrom('asdr1@yahoo.com')
          ->addTo('asdr1@yahoo.com')
          ->setSubject('New contact')
          ->setHtmlBody('Hi<br> you\'ve got new message.<br> from: ' . $values['name'] . '<br> email: ' . $values['email'] . '<br> Message:' . $values['message']);

  $mailer = new Nette\Mail\SendmailMailer();
  $mailer->send($mail);

how i set SMTP for gmail我如何将 SMTP 设置为 gmail

public $defaults = [
        'smtp' => false,
        'host' => null,
        'port' => null,
        'username' => null,
        'password' =>  null,
        'secure' => null,
        'timeout' => null,
        'clientHost' => null,
    ];

SendmailMailer uses php function mail() and it has to be configured in php.ini to work, see https://www.php.net/manual/en/mail.configuration.php SendmailMailer使用 php function mail()并且它必须在php.ini中配置才能工作,参见https://www.php.net/manual/en/mail.configuration.php

Simplest way of using smtp with Nette is SmtpMailer将 smtp 与 Nette 一起使用的最简单方法是SmtpMailer

$mailer = new \Nette\Mail\SmtpMailer($this->defaults);

Also don't forget to set correct configuration, gmail will not accept empty credentials.也不要忘记设置正确的配置,gmail 不会接受空凭据。 Something like:就像是:

public $defaults = [
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'username' => 'example@gmail.com',
    'password' =>  '****',
    'secure' => 'ssl',
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM