Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/l10n/class-wp-translation-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/l10n/class-wp-translation-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-object-cache.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-object-cache.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-dependencies.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-dependencies.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-http.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/class-wp-http.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/nav-menu.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/nav-menu.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/block-editor.php on line 1
Warning: Uninitialized string offset 0 in /home2/zhaff/public_html/blogneptunescripts/wp-includes/block-editor.php on line 1
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
Warning: Cannot modify header information - headers already sent by (output started at /home2/zhaff/public_html/blogneptunescripts/wp-includes/default-constants.php:1) in /home2/zhaff/public_html/blogneptunescripts/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":85,"date":"2016-11-22T08:41:49","date_gmt":"2016-11-22T00:41:49","guid":{"rendered":"http:\/\/blog.neptunescripts.com\/?p=85"},"modified":"2016-11-22T18:58:34","modified_gmt":"2016-11-22T10:58:34","slug":"use-smtp-email-settings-from-database-in-cakephp-2","status":"publish","type":"post","link":"https:\/\/blog.neptunescripts.com\/2016\/11\/22\/use-smtp-email-settings-from-database-in-cakephp-2\/","title":{"rendered":"Use SMTP Email Settings from Database in CakePHP 2"},"content":{"rendered":"Yesterday, one of our client requested to store SMTP email settings in database. Usually, we can do it in the email.php file. However,\u00a0it is more convenient if we store it in the database and provide a\u00a0web form to easily change\u00a0it.<\/p>\n
Let say we store the email settings in table called email_settings with model EmailSetting.<\/p>\n
One of the way is to retrieve the email settings record in every email sending method you have\u00a0as per below:<\/p>\n
\/\/ get email settings\r\n$EmailSetting = ClassRegistry::init('EmailSetting');\r\n$email_settings = $EmailSetting->findById(1);\r\n$email_setting = $email_settings['EmailSetting'];<\/pre>\nand load the SMTP email settings as per below:<\/p>\n
if (!empty($email_setting['smtp_host'])) {\r\n $email->config(array(\r\n 'host' => $email_setting['smtp_host'],\r\n 'port' => $email_setting['smtp_port'],\r\n 'username' => $email_setting['smtp_user'],\r\n 'password' => $email_setting['smtp_password'],\r\n ));\r\n}<\/pre>\nBut that way will not promote a code use. After digging CakePHP cookbook, we found out that we can retrieve the email settings in the EmailConfig class (in file \/app\/Config\/email.php). You can just load the SMTP email settings in\u00a0__construct() function. It’s very convenient and you can easily change it whenever needed.<\/p>\n
Here’s the codes:<\/p>\n
File:\u00a0 \/app\/Config\/email.php\r\n\r\nclass EmailConfig\r\n{\r\n public function __construct()\r\n {\r\n $EmailSetting = ClassRegistry::init('EmailSetting');\r\n $email_settings = $EmailSetting->findById(1);\r\n $email_setting = $email_settings['EmailSetting'];\r\n\r\n if (!empty($email_setting['smtp_host'])) {\r\n $this->default = array(\r\n 'transport' => 'Smtp',\r\n 'from' => $email_setting['from'],\r\n 'host' => $email_setting['smtp_host'],\r\n 'port' => $email_setting['smtp_port'],\r\n 'timeout' => 30,\r\n 'username' => $email_setting['smtp_user'],\r\n 'password' => $email_setting['smtp_password'],\r\n 'client' => null\r\n );\r\n }\r\n }\r\n}<\/pre>\nEnjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"
Yesterday, one of our client requested to store SMTP email settings in database. Usually, we can do it in the email.php file. However,\u00a0it is more convenient if we store it in the database and provide a\u00a0web form to easily change\u00a0it. Let say we store the email settings in table called email_settings with model EmailSetting. One … Continue reading Use SMTP Email Settings from Database in CakePHP 2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,34,35],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cakephp","tag-cakephp","tag-email","tag-smtp"],"_links":{"self":[{"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/posts\/85\/revisions\/97"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.neptunescripts.com\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}