【问题标题】:Can swiftMailer be used on localhost to send emails?可以在 localhost 上使用 swiftMailer 发送电子邮件吗?
【发布时间】:2015-03-01 01:25:49
【问题描述】:

我目前没有收到使用以下配置的电子邮件,我想知道这是否与我的设置有关,可能缺少某些内容,或者它在 MAMP localhost 上不起作用?

公共配置目录下的main-local.php

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        'useFileTransport' => true,
    ],

然后发送电子邮件(显示成功消息)

public function submitreview($email)
{
    //return Yii::$app->mailer->compose(['html' => '@app/mail-templates/submitreview'])
    return Yii::$app->mailer->compose()
        ->setTo($email)
        ->setFrom([$this->email => $this->name])
        ->setSubject($this->title)
        ->setTextBody($this->description)
        ->attachContent($this->file)
        ->send();
}

【问题讨论】:

  • swiftMailer 应该在 localhost 上工作,但您需要指定您的 ISP 邮件服务器,并可能使用用户名和密码。我会参考stackoverflow.com/questions/4485635/…google.com.au/#q=swiftmailer+localhost
  • 但是,如果您的意思是使用 localhost 作为邮件服务器,而不是在 localhost 上托管您的网站并通过 gmail 邮件服务器发送邮件。那么您将需要像水银邮件这样的东西来充当邮件服务器。

标签: php yii2 swiftmailer


【解决方案1】:

你可以通过 Yii2 中的 localhost 发送邮件,配置如下。

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',  
            'username' => 'ENTER_EMAIL_ADDRESS_HERE',
            'password' => 'ENTER_PASSWORD',
            'port' => '587', 
            'encryption' => 'tls', 
        ],
    ]

在你的控制器中

\Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('to_email@xx.com')
    ->setSubject('This is a test mail ' )
    ->send();

【讨论】:

  • 这个本地主机怎么样?您列出的传输方式是 gmail。
【解决方案2】:

我只是使用 gmail 进行测试,使用这个 php 文件从本地主机发送邮件。 进行生产时,请将传输文件替换为您的原始凭据。

如果邮件发送成功,$result 将回显 1

<?php
            $subject="Testing Mail";
            $body="<h2>This is the body</h2>";
            $to="*******@gmail.com";  //this is the to email address

            require_once 'swiftmailer/swift_required.php';
            // Create the mail transport configuration

            $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('######')->setPassword('######');
            //$transport = Swift_MailTransport::newInstance();
            $message = Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom(array('######@gmail.com'))
            ->setTo(array($to))
            ->setBody($body,'text/html');

            //send the message 
            $mailer = Swift_Mailer::newInstance($transport);         
            $result=$mailer->send($message);

    ?>

【讨论】:

    【解决方案3】:

    useFileTransport 设置为true(开发环境中的默认值)时,邮件将作为文件保存在“运行时”文件夹中。

    例如,如果您在网站后端使用高级入门模板并以用户身份注册(并使用发送用户注册电子邮件的扩展程序),则注册电子邮件将保存在/backend/runtime/mail

    【讨论】:

    • 没错。这是来自documentation about useFileTransport 的引用:“是否将电子邮件消息保存为 $fileTransportPath 下的文件而不是将它们发送给实际的收件人。这通常在开发期间用于调试目的。” (强调我的)
    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2010-12-11
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多