doudonglu3764 2014-11-13 03:08
浏览 32
已采纳

如何在zend框架中的电子邮件的bootstrap文件中发送动态数据

I am sending email to user while registration. For that i am using Zend's default function. For that i added code in my Bootstrap file.

protected function _initMail()
    {
        try {
            $config = array(
                'auth' => 'login',
                'username' => 'username@gmail.com',
                'password' => 'password',
                'ssl' => 'tls',
                'port' => 587
            );
            $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
            Zend_Mail::setDefaultTransport($mailTransport);
        } catch (Zend_Exception $e){
        }
    }

Now my need is username,password and port i want to make this field dynamic. I want to fetch this record from the database. Can anyone please tell me how can i solve that problem. Any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • drbvm26000 2014-11-13 19:43
    关注

    I would say that you do not put these into the database. A better way would be to put them into a config file and then set them up from there. Doing a database call EVERY SINGLE time your application is called is not really optimal.

    The way you should be doing it is:

    protected function _initMail()
        {
            try {
                $config = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    
                $config = $config->getOption("mail");
                $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
                Zend_Mail::setDefaultTransport($mailTransport);
            } catch (Zend_Exception $e){
            }
        }
    

    That is assuming that your application.ini has this:

    mail.auth="login"
    mail.username="username@gmail.com"
    mail.password="password"
    mail.ssl="tls"
    mail.port=587
    

    However if you decide you want to do this, and have set up your database in the application.ini file (assuming you use ini files for config) something like this:

    resources.db.adapter = "PDO_MYSQL"
    resources.db.params.host = "your.database.host"
    resources.db.params.dbname = "database_name"
    resources.db.params.username = "username"
    resources.db.params.password = "password"
    resources.db.isDefaultTableAdapter = true
    

    You can then do:

    protected function _initMail()
    {
        $resource = $bootstrap->getPluginResource('db');
        $db = $resource->getDbAdapter();
        $select=$db->select()->from("emailSetup");
        $credentials=$db->fetchOne($select);
        [..email part here..]
    }
    

    P.S. I have not tested the select, but you should get the gist of it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记