{"id":73,"date":"2023-08-20T08:03:02","date_gmt":"2023-08-20T08:03:02","guid":{"rendered":"https:\/\/mailmug.net\/blog\/?p=73"},"modified":"2025-11-25T07:50:30","modified_gmt":"2025-11-25T07:50:30","slug":"send-email-from-yii2","status":"publish","type":"post","link":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/","title":{"rendered":"Framework YII2 Send Email Example"},"content":{"rendered":"\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to send emails from the yii2 php framework?<\/p>\n\n\n\n<p>Instructions to configure email SMTP with the yii2 framework (yii2 send email example). The PHP yii2 framework has <a href=\"https:\/\/github.com\/yiisoft\/yii2-symfonymailer\">yiisoft\/yii2-symfonymailer<\/a> library. No need to install this package again. <\/p>\n\n\n\n<div style=\"height:77px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"YII2 Framework Send Email\" width=\"640\" height=\"480\" src=\"https:\/\/www.youtube.com\/embed\/Uj3HudH_XDU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Configure Email SMTP credentials <\/h2>\n\n\n\n<p>Modify <code>config\/web.php<\/code> file. Now, we need to edit the username and password of the SMTP server. <\/p>\n\n\n\n<p><strong>Where can I get SMTP credentials for testing?<\/strong><\/p>\n\n\n\n<p>Just create <strong><a href=\"https:\/\/mailmug.net\/\">MailMug.net<\/a><\/strong> free account. Then you can see the SMTP sandbox account credentials from the MailMug.net <strong>dashboard &gt; Settings<\/strong> page.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"262\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials.png\" alt=\"\" class=\"wp-image-75\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials.png 631w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials-300x125.png 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/figure><\/div>\n\n\n<div style=\"height:48px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Where can I get SMTP credentials for the production server?<\/strong><\/p>\n\n\n\n<p>Create an email account for your domain name from <strong>cPanel<\/strong> or <strong>Plesk<\/strong> or any other server <strong>control panel<\/strong>. Then you can see SMTP credentials. Just copy and paste credentials to the <strong>config\/web.php<\/strong> file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>The config\/web.php file<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&#039;components&#039; =&gt; &#x5B;\n        \/\/....\n        &#039;mailer&#039; =&gt; &#x5B;\n            &#039;class&#039; =&gt; \\yii\\symfonymailer\\Mailer::class,            \n            &#039;transport&#039; =&gt; &#x5B;\n                &#039;scheme&#039; =&gt; &#039;smtp&#039;,\n                &#039;host&#039; =&gt; &#039;smtp.mailmug.net&#039;,\n                &#039;username&#039; =&gt; &#039;lz5ebosxdq0uruxb&#039;,\n                &#039;password&#039; =&gt; &#039;kbs6m4hifn8di3gi&#039;,\n                &#039;port&#039; =&gt; 2525,\n            ],\n            &#039;viewPath&#039; =&gt; &#039;@app\/mail&#039;,\n            \/\/ send all mails to a file by default. You have to set\n            \/\/ &#039;useFileTransport&#039; to false and configure transport\n            \/\/ for the mailer to send real emails.\n            &#039;useFileTransport&#039; =&gt; false,\n        ],\n       \/\/..\n ];\n<\/pre><\/div>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Test it<\/h2>\n\n\n\n<p>You can send emails from controllers or models. Check the following code.  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nnamespace app\\controllers;\nuse Yii;\n\nclass PostController extends \\yii\\web\\Controller\n{\n    public function actionIndex()\n    {\n        $this-&gt;send();\n        return $this-&gt;render(&#039;index&#039;);\n    }\n\n    public function send()\n    {\n        Yii::$app-&gt;mailer-&gt;compose()\n                -&gt;setTo(&#039;your@email.com&#039;)\n                -&gt;setFrom(&#x5B;Yii::$app-&gt;params&#x5B;&#039;senderEmail&#039;] =&gt; Yii::$app-&gt;params&#x5B;&#039;senderName&#039;]])\n                -&gt;setSubject(&#039;Test Subject&#039;)\n                -&gt;setTextBody(&#039;Test Body&#039;)\n                -&gt;send();\n    }\n\n}\n<\/pre><\/div>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>How to add an HTML email template from the Yii framework?<\/strong><\/p>\n\n\n\n<p>The HTML email template is already available in the <code>mail\/layouts<\/code> folder. Pass email template name to <code>compose<\/code> method. <\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n Yii::$app-&gt;mailer-&gt;compose(&#039;layouts\/html&#039;, &#x5B;&#039;content&#039; =&gt; &#039;Some data here&#039;])\n                -&gt;setTo(&#039;your@email.com&#039;)\n                -&gt;setFrom(&#x5B;Yii::$app-&gt;params&#x5B;&#039;senderEmail&#039;] =&gt; Yii::$app-&gt;params&#x5B;&#039;senderName&#039;]])\n                -&gt;setSubject(&#039;Test Subject&#039;)\n                -&gt;send();\n<\/pre><\/div>\n\n\n<div style=\"height:64px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>How to send an email with an attachment?<\/strong><\/p>\n\n\n\n<p>Pass the file name with the path in  <code>attach<\/code> method.<\/p>\n\n\n\n<p><strong>Example<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n Yii::$app-&gt;mailer-&gt;compose(&#039;layouts\/html&#039;, &#x5B;&#039;content&#039; =&gt; &#039;Some data here&#039;])\n                -&gt;setTo(&#039;your@email.com&#039;)\n                -&gt;setFrom(&#x5B;Yii::$app-&gt;params&#x5B;&#039;senderEmail&#039;] =&gt; Yii::$app-&gt;params&#x5B;&#039;senderName&#039;]])\n                -&gt;setSubject(&#039;Test Subject&#039;)\n\n                -&gt;attach(&#039;\/path\/drawing.svg&#039;, &#x5B;&#039;fileName&#039; =&gt; &#039;drawing.svg&#039;])\n                -&gt;attach(&#039;\/path\/drawing1.svg&#039;, &#x5B;&#039;fileName&#039; =&gt; &#039;drawing1.svg&#039;])\n\n                -&gt;send();\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>How to send emails from the yii2 php framework? Instructions to configure email SMTP with the yii2 framework (yii2 send email example). The PHP yii2 framework has yiisoft\/yii2-symfonymailer library. No need to install this package again. Step 1: Configure Email SMTP credentials Modify config\/web.php file. Now, we need to edit the username and password of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":74,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Framework YII2 Send Email Example - MailMug Blog<\/title>\n<meta name=\"description\" content=\"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Framework YII2 Send Email Example - MailMug Blog\" \/>\n<meta property=\"og:description\" content=\"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\" \/>\n<meta property=\"og:site_name\" content=\"MailMug Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-20T08:03:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-25T07:50:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/email-yii.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d\"},\"headline\":\"Framework YII2 Send Email Example\",\"datePublished\":\"2023-08-20T08:03:02+00:00\",\"dateModified\":\"2025-11-25T07:50:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\"},\"wordCount\":211,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\"},\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\",\"url\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\",\"name\":\"Framework YII2 Send Email Example - MailMug Blog\",\"isPartOf\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#website\"},\"datePublished\":\"2023-08-20T08:03:02+00:00\",\"dateModified\":\"2025-11-25T07:50:30+00:00\",\"description\":\"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).\",\"breadcrumb\":{\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mailmug.net\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Framework YII2 Send Email Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mailmug.net\/blog\/#website\",\"url\":\"https:\/\/mailmug.net\/blog\/\",\"name\":\"MailMug\",\"description\":\"SandBox SMTP Email Account\",\"publisher\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mailmug.net\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\",\"name\":\"MailMug\",\"url\":\"https:\/\/mailmug.net\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png\",\"contentUrl\":\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png\",\"width\":320,\"height\":293,\"caption\":\"MailMug\"},\"image\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/mailmug.net\/blog\"],\"url\":\"https:\/\/mailmug.net\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Framework YII2 Send Email Example - MailMug Blog","description":"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/","og_locale":"en_US","og_type":"article","og_title":"Framework YII2 Send Email Example - MailMug Blog","og_description":"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).","og_url":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/","og_site_name":"MailMug Blog","article_published_time":"2023-08-20T08:03:02+00:00","article_modified_time":"2025-11-25T07:50:30+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/email-yii.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#article","isPartOf":{"@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/"},"author":{"name":"admin","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d"},"headline":"Framework YII2 Send Email Example","datePublished":"2023-08-20T08:03:02+00:00","dateModified":"2025-11-25T07:50:30+00:00","mainEntityOfPage":{"@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/"},"wordCount":211,"commentCount":0,"publisher":{"@id":"https:\/\/mailmug.net\/blog\/#organization"},"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/","url":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/","name":"Framework YII2 Send Email Example - MailMug Blog","isPartOf":{"@id":"https:\/\/mailmug.net\/blog\/#website"},"datePublished":"2023-08-20T08:03:02+00:00","dateModified":"2025-11-25T07:50:30+00:00","description":"Configure email SMTP with the yii2 framework. The PHP yii2 framework has yiisoft\/yii2-symfonymailer library (YII2 send email).","breadcrumb":{"@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mailmug.net\/blog\/send-email-from-yii2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mailmug.net\/blog\/send-email-from-yii2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mailmug.net\/blog\/"},{"@type":"ListItem","position":2,"name":"Framework YII2 Send Email Example"}]},{"@type":"WebSite","@id":"https:\/\/mailmug.net\/blog\/#website","url":"https:\/\/mailmug.net\/blog\/","name":"MailMug","description":"SandBox SMTP Email Account","publisher":{"@id":"https:\/\/mailmug.net\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mailmug.net\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/mailmug.net\/blog\/#organization","name":"MailMug","url":"https:\/\/mailmug.net\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png","contentUrl":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png","width":320,"height":293,"caption":"MailMug"},"image":{"@id":"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/mailmug.net\/blog"],"url":"https:\/\/mailmug.net\/blog\/author\/admin\/"}]}},"amp_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":6,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions\/299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/media\/74"}],"wp:attachment":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}