使用gmail从localhost发送电子邮件

时间:2022-09-04 18:13:52

I'm developing a website using cakephp 3.0 and I'm trying to send a confirmation email from my localhost (xampp) by using gmail server. I can tell for sure that my controller function is executed but nothing happen, no email, no error, no log, etc.

我正在使用cakephp 3.0开发一个网站,我正在尝试使用gmail服务器从我的localhost(xampp)发送确认电子邮件。我可以确定我的控制器功能已执行但没有任何反应,没有电子邮件,没有错误,没有日志等。

I read here that you can't send email from localhost (hence trying to use gmail to send it)

我在这里读到你无法从localhost发送电子邮件(因此尝试使用gmail发送它)

Here my config in the app.php

这是我在app.php中的配置

'EmailTransport' => [
    'default' => [
        'className' => 'Mail',
        // The following keys are used in SMTP transports
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'timeout' => 30,
        'username' => 'my_email@gmail.com',
        'password' => 'password',
        'client' => null,
        'tls' => true,
    ],
],

'Email' => [
    'default' => [
        'transport' => 'default',
        'from' => 'my_email@gmail.com',
    ],
],

And here is my function where I try to send the email.

这是我的功能,我尝试发送电子邮件。

public function send()
{
    $email = new Email('default');
    $email->to('other_email@gmail')
          ->subject('About')
          ->message('blablabla');

    if($email->send())
    {
        return $this->render('confirmation');
    }
}

I can tell for sure this code is executed because the confirmation view is rendered after I press send.

我可以确定这个代码是执行的,因为在我按下send后会显示确认视图。

What am I missing to send the email?

发送电子邮件我错过了什么?

2 个解决方案

#1


0  

You won't be able to send an email without a server that has a mail server. This unfortunately, is not included with xampp. If you're trying to send emails from your Gmail using the "From" header, I think that you are misunderstanding the use of the "From" parameter. The "From" is really only for the receiving parties end.

没有带有邮件服务器的服务器,您将无法发送电子邮件。不幸的是,这不包括在xampp中。如果您尝试使用“发件人”标题从Gmail发送电子邮件,我认为您误解了“发件人”参数的使用。 “来自”实际上只适用于接收方。

Example:

If you have:

如果你有:

$headers = "From: webmaster@example.com";

The receiving party will see the email was sent from webmaster@example.com. However, the email is actually still sent from YOUR mail server (so on localhost, you would be unable to do so, as it does not have a mail server).

接收方将看到该电子邮件是从webmaster@example.com发送的。但是,电子邮件实际上仍然是从您的邮件服务器发送的(因此在localhost上,您将无法这样做,因为它没有邮件服务器)。

For more information on sending emails with PHP, go to http://www.w3schools.com/php/func_mail_mail.asp

有关使用PHP发送电子邮件的更多信息,请访问http://www.w3schools.com/php/func_mail_mail.asp

#2


0  

Try with

 'EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'timeout' => 30,
        'username' => 'my_email@gmail.com',
        'password' => 'password',
        'tls' => true, // This may not be needed
    ],
],

#1


0  

You won't be able to send an email without a server that has a mail server. This unfortunately, is not included with xampp. If you're trying to send emails from your Gmail using the "From" header, I think that you are misunderstanding the use of the "From" parameter. The "From" is really only for the receiving parties end.

没有带有邮件服务器的服务器,您将无法发送电子邮件。不幸的是,这不包括在xampp中。如果您尝试使用“发件人”标题从Gmail发送电子邮件,我认为您误解了“发件人”参数的使用。 “来自”实际上只适用于接收方。

Example:

If you have:

如果你有:

$headers = "From: webmaster@example.com";

The receiving party will see the email was sent from webmaster@example.com. However, the email is actually still sent from YOUR mail server (so on localhost, you would be unable to do so, as it does not have a mail server).

接收方将看到该电子邮件是从webmaster@example.com发送的。但是,电子邮件实际上仍然是从您的邮件服务器发送的(因此在localhost上,您将无法这样做,因为它没有邮件服务器)。

For more information on sending emails with PHP, go to http://www.w3schools.com/php/func_mail_mail.asp

有关使用PHP发送电子邮件的更多信息,请访问http://www.w3schools.com/php/func_mail_mail.asp

#2


0  

Try with

 'EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'timeout' => 30,
        'username' => 'my_email@gmail.com',
        'password' => 'password',
        'tls' => true, // This may not be needed
    ],
],