{"id":20540,"date":"2022-09-26T00:00:00","date_gmt":"2022-09-26T00:00:00","guid":{"rendered":"https:\/\/www.mailgun.com\/blog\/php-como-usar-mailgun-eficiencia\/"},"modified":"2026-09-15T03:52:50","modified_gmt":"2026-09-15T03:52:50","slug":"php-como-usar-mailgun-eficiencia","status":"publish","type":"blog","link":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/","title":{"rendered":"Como preparar o Mailgun rapidamente com PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When I started coding, I remember debating whether I would want to learn ASP.NET vs PHP. I read thousands of bulletin boards on threads about each language to help me make my decision.<\/p>\n\n<p class=\"wp-block-paragraph\">I chose PHP mainly for the community, the availability of examples and for the fact that it was open source. I\u2019m sure these are similar reasons why you\u2019ve chosen to adventure yourself in the world of PHP.<\/p>\n\n<p class=\"wp-block-paragraph\">PHP is one of the most widely used languages, yet there is a lot of confusing or outdated material on the web.<\/p>\n\n<p class=\"wp-block-paragraph\"><strong>The aim of this particular post is to provide you with a detailed overview on how to use Mailgun in your PHP environment with simple copy paste examples<\/strong><\/p>\n\r\n    <nav data-content-type=\"longform\" class=\"toc-block longform-spacings px-5 py-6 px-md-6 px-lg-7 py-md-7 bg-light fs-sm rounded-lg\" aria-labelledby=\"toc-title-6708\"><p class=\"h5 m-0\" id=\"toc-title-6708\">\u00cdndice<\/p><div class=\"d-flex mt-3\"><div class=\"w-auto fw-bold text-accent d-flex me-2\">01<\/div><div class=\"d-flex flex-column\"><a class=\"fw-bold scrollme link-body-color text-accent\" href=\"#using-the-official-mailgun-php-library\">Using the official Mailgun-PHP library<\/a><a class=\"scrollme link-body-color\" href=\"#steps\">Steps<\/a><\/div><\/div><div class=\"d-flex mt-3\"><div class=\"w-auto fw-bold text-accent d-flex me-2\">02<\/div><div class=\"d-flex flex-column\"><a class=\"fw-bold scrollme link-body-color text-accent\" href=\"#using-swift-mailer\">Using Swift Mailer<\/a><a class=\"scrollme link-body-color\" href=\"#steps\">Steps<\/a><\/div><\/div><div class=\"d-flex mt-3\"><div class=\"w-auto fw-bold text-accent d-flex me-2\">03<\/div><div class=\"d-flex flex-column\"><a class=\"fw-bold scrollme link-body-color text-accent\" href=\"#using-php-mailer\">Using PHP Mailer<\/a><a class=\"scrollme link-body-color\" href=\"#steps\">Steps<\/a><\/div><\/div><div class=\"d-flex mt-3\"><div class=\"w-auto fw-bold text-accent d-flex me-2\">04<\/div><div class=\"d-flex flex-column\"><a class=\"fw-bold scrollme link-body-color text-accent\" href=\"#baking-is-awesome\">Baking is awesome<\/a><\/div><\/div><\/nav>\n<h2 class=\"wp-block-heading\">Using the official Mailgun-PHP library<\/h2>\n\n<p class=\"wp-block-paragraph\">A very straightforward way to use Mailgun and leverage its outstanding API to deliver emails is to install the official PHP library in your development environment, written and maintained\u00a0<a href=\"https:\/\/github.com\/mailgun\/mailgun-php\" target=\"_tabs\" rel=\"noopener noreferrer\">as an open source project<\/a>\u00a0by our Mailgunner Travis.<\/p>\n\n<h3 class=\"wp-block-heading\">Steps<\/h3>\n\n<ul class=\"wp-block-list\">\n<li>Open your terminal and navigate to your server\u2019s public folder. This is where you store your website and varies depending on installation and web-server used &#8211; <code>cd \/var\/www\/html<\/code><\/li>\n\n\n\n<li>Create an empty directory for this project &#8211; <code>mkdir mailgun-php<\/code><\/li>\n\n\n\n<li>Enter the directory and execute the following commands &#8211; <code>cd mailgun-php <\/code>(changes directory)<\/li>\n\n\n\n<li>Now execute the following command to download and install Composer. &#8211; <code>curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/li>\n<\/ul>\n\n<p class=\"wp-block-paragraph\">Composer, when used this way, allows us to download PHP libraries with ease locally within the folder. Now let\u2019s use composer to <b>install the Mailgun library itself<\/b>.<\/p>\n\n<ul class=\"wp-block-list\">\n<li>Let\u2019s run this to tell composer we\u2019re going to use (and therefore we need it downloaded) the mailgun library (here we use 1.7.1) and its dependencies. &#8211; <code>php composer.phar require mailgun\/mailgun-php:~1.7.1<\/code><\/li>\n<\/ul>\n\n<p class=\"wp-block-paragraph\">Congratulations, you\u2019ve installed the Mailgun official library and you are now ready to <b>write some code<\/b>!<\/p>\n\n<p class=\"wp-block-paragraph\">Open a file and include the\u00a0<code>vendor\/autoload.php <\/code>file to get started. All the examples below assume you&#8217;ve already included this in your file:<\/p>\n    <div data-content-type=\"longform\" class=\"code-snippet longform-spacings rounded-lg overflow-hidden shadow \" data-count=\"1\">\r\n        <ul class=\"nav nav-buttons code-snippet__tabs longform-except position-relative d-flex gap-2 flex-wrap p-3\" role=\"tablist\">\r\n\r\n                            \r\n                    \r\n                        <li class=\"nav-item\" role=\"presentation\">\r\n                            <button class=\"nav-link active\" data-bs-toggle=\"tab\" data-bs-target=\"#code_0\" type=\"button\" role=\"tab\" aria-controls=\"code_0\" aria-selected=\"true\">\r\n                                PHP                            <\/button>\r\n                        <\/li>\r\n\r\n                    \r\n                            \r\n        <\/ul>\r\n        <div class=\"code-snippet__tab-content tab-content\">\r\n\r\n                            \r\n                    <div class=\"tab-pane show active\" id=\"code_0\" role=\"tabpanel\" aria-labelledby=\"code_0\">\r\n                        <div class=\"code-snippet__code-wrapper\">\r\n                            <div class=\"nav nav-buttons code-snippet__copy-wrapper\">\r\n                                <button type=\"button\" class=\"nav-link code-snippet__copy\" data-clipboard-target=\"#code_0_content\" data-clipboard-label=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-clipboard-success-label=\"Copiado!\" data-clipboard-error-label=\"N\u00e3o foi poss\u00edvel copiar\" data-tooltip=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-tooltip-placement=\"left\" role=\"tooltip\" aria-label=\"Copiar para a \u00e1rea de transfer\u00eancia\">\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--default\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/file-stack.svg\"><\/svg>\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--success\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/badge-check.svg\"><\/svg>\r\n                                <\/button>\r\n                            <\/div>\r\n                            <pre class=\"w-100 h-100 m-0 line-numbers language-PHP\">\r\n                                <code id=\"code_0_content\" class=\"language-PHP\">\r\n\r\n                                    \r\n                                <\/code>\r\n                            <\/pre>\r\n                        <\/div>\r\n                    <\/div>\r\n                                    <\/div>\r\n    <\/div>\r\n\n<p class=\"wp-block-paragraph\">Then, <a href=\"https:\/\/github.com\/mailgun\/mailgun-php\" target=\"_tabs\" rel=\"noopener noreferrer\">follow these instructions from the official documentation<\/a> to send a message using the SDK:<\/p>\n    <div data-content-type=\"longform\" class=\"code-snippet longform-spacings rounded-lg overflow-hidden shadow \" data-count=\"1\">\r\n        <ul class=\"nav nav-buttons code-snippet__tabs longform-except position-relative d-flex gap-2 flex-wrap p-3\" role=\"tablist\">\r\n\r\n                            \r\n                    \r\n                        <li class=\"nav-item\" role=\"presentation\">\r\n                            <button class=\"nav-link active\" data-bs-toggle=\"tab\" data-bs-target=\"#code_0\" type=\"button\" role=\"tab\" aria-controls=\"code_0\" aria-selected=\"true\">\r\n                                PHP                            <\/button>\r\n                        <\/li>\r\n\r\n                    \r\n                            \r\n        <\/ul>\r\n        <div class=\"code-snippet__tab-content tab-content\">\r\n\r\n                            \r\n                    <div class=\"tab-pane show active\" id=\"code_0\" role=\"tabpanel\" aria-labelledby=\"code_0\">\r\n                        <div class=\"code-snippet__code-wrapper\">\r\n                            <div class=\"nav nav-buttons code-snippet__copy-wrapper\">\r\n                                <button type=\"button\" class=\"nav-link code-snippet__copy\" data-clipboard-target=\"#code_0_content\" data-clipboard-label=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-clipboard-success-label=\"Copiado!\" data-clipboard-error-label=\"N\u00e3o foi poss\u00edvel copiar\" data-tooltip=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-tooltip-placement=\"left\" role=\"tooltip\" aria-label=\"Copiar para a \u00e1rea de transfer\u00eancia\">\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--default\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/file-stack.svg\"><\/svg>\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--success\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/badge-check.svg\"><\/svg>\r\n                                <\/button>\r\n                            <\/div>\r\n                            <pre class=\"w-100 h-100 m-0 line-numbers language-PHP\">\r\n                                <code id=\"code_0_content\" class=\"language-PHP\">\r\n\r\n                                    messages()-&gt;send($domain, $params);\r\n$mg-&gt;messages()-&gt;send(&#039;example.com&#039;, [\r\n  &#039;from&#039;    =&gt; &#039;bob@example.com&#039;,\r\n  &#039;to&#039;      =&gt; &#039;sally@example.com&#039;,\r\n  &#039;subject&#039; =&gt; &#039;The PHP SDK is awesome!&#039;,\r\n  &#039;text&#039;    =&gt; &#039;It is so simple to send a message.&#039;\r\n]);\r\n?&gt;\r\n                                <\/code>\r\n                            <\/pre>\r\n                        <\/div>\r\n                    <\/div>\r\n                                    <\/div>\r\n    <\/div>\r\n\n<p class=\"wp-block-paragraph\"><b>Remember: <\/b><code>$domain<\/code> must match to the domain you have configured on <a href=\"https:\/\/app.mailgun.com\/app\/domains\" target=\"_tabs\" rel=\"noopener noreferrer\">app.mailgun.com<\/a>.<\/p>\n\n<h2 class=\"wp-block-heading\">Using Swift Mailer<\/h2>\n\n<p class=\"wp-block-paragraph\">Using Mailgun with\u00a0<a href=\"https:\/\/github.com\/swiftmailer\/swiftmailer\" target=\"_tabs\" rel=\"noopener noreferrer\">Swift Mailer<\/a>\u00a0is a breeze.<\/p>\n\n<p class=\"wp-block-paragraph\">Make sure you have your domain\u2019s SMTP credentials to hand! You can\u00a0<a href=\"https:\/\/app.mailgun.com\/sessions\/new\" target=\"_tabs\" rel=\"noopener noreferrer\">find these here<\/a>\u00a0they\u2019re named:<\/p>\n\n<ul class=\"wp-block-list\">\n<li>Default SMTP Login<\/li>\n\n\n\n<li>Default Password<\/li>\n<\/ul>\n\n<h3 class=\"wp-block-heading\">Steps<\/h3>\n\n<ul class=\"wp-block-list\">\n<li>Create a folder called <i>Swiftmailer &#8211; <\/i><code>mkdir swiftmailer, cd swiftmailer<\/code><\/li>\n\n\n\n<li>Download Composer to install <i>swiftmailer <\/i>and its dependencies\n<ul class=\"wp-block-list\">\n<li><code>curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Install <i>Swiftmailer<\/i>\n<ul class=\"wp-block-list\">\n<li><code>php composer.phar require swiftmailer\/swiftmailer @stable<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Files will be downloaded, once finished simply create your own empty file to test that phpmailer works. Within your terminal you can achieve that by doing:\n<ul class=\"wp-block-list\">\n<li><code>touch mailgun_with_swiftmailer.php<\/code><\/li>\n\n\n\n<li><code>vim mailgun_with_swiftmailer.php<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n<p class=\"wp-block-paragraph\">Here we\u2019re using vim, but you can edit the file\u00a0<code>mailgun_with_swiftmailer.php<\/code>\u00a0in whichever editor you feel most comfortable with.<\/p>\n    <div data-content-type=\"longform\" class=\"code-snippet longform-spacings rounded-lg overflow-hidden shadow \" data-count=\"1\">\r\n        <ul class=\"nav nav-buttons code-snippet__tabs longform-except position-relative d-flex gap-2 flex-wrap p-3\" role=\"tablist\">\r\n\r\n                            \r\n                    \r\n                        <li class=\"nav-item\" role=\"presentation\">\r\n                            <button class=\"nav-link active\" data-bs-toggle=\"tab\" data-bs-target=\"#code_0\" type=\"button\" role=\"tab\" aria-controls=\"code_0\" aria-selected=\"true\">\r\n                                PHP                            <\/button>\r\n                        <\/li>\r\n\r\n                    \r\n                            \r\n        <\/ul>\r\n        <div class=\"code-snippet__tab-content tab-content\">\r\n\r\n                            \r\n                    <div class=\"tab-pane show active\" id=\"code_0\" role=\"tabpanel\" aria-labelledby=\"code_0\">\r\n                        <div class=\"code-snippet__code-wrapper\">\r\n                            <div class=\"nav nav-buttons code-snippet__copy-wrapper\">\r\n                                <button type=\"button\" class=\"nav-link code-snippet__copy\" data-clipboard-target=\"#code_0_content\" data-clipboard-label=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-clipboard-success-label=\"Copiado!\" data-clipboard-error-label=\"N\u00e3o foi poss\u00edvel copiar\" data-tooltip=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-tooltip-placement=\"left\" role=\"tooltip\" aria-label=\"Copiar para a \u00e1rea de transfer\u00eancia\">\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--default\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/file-stack.svg\"><\/svg>\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--success\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/badge-check.svg\"><\/svg>\r\n                                <\/button>\r\n                            <\/div>\r\n                            <pre class=\"w-100 h-100 m-0 line-numbers language-PHP\">\r\n                                <code id=\"code_0_content\" class=\"language-PHP\">\r\n\r\n                                    setUsername(&#039;your SMTP username&#039;)\r\n  -&gt;setPassword(&#039;your SMTP password&#039;)\r\n  ;\r\n\r\n\/\/ Create the Mailer using your created Transport\r\n$mailer = Swift_Mailer::newInstance($transport);\r\n\r\n\/\/ Create a message\r\n$message = Swift_Message::newInstance(&#039;Hello from Mailgun&#039;)\r\n  -&gt;setFrom(array(&#039;someone@domain.com&#039; =&gt; &#039;John Doe&#039;))\r\n  -&gt;setTo(array(&#039;someone_else@domain.org&#039;, &#039;third_contact@domain.org&#039; =&gt; &#039;A third name&#039;))\r\n  -&gt;setBody(&quot;And that&#039;s another ace in the hole for the Mailgun dev!&quot;)\r\n  ;\r\n\r\n\/\/ Send the message\r\n$result = $mailer-&gt;send($message);\r\n?&gt;\r\n                                <\/code>\r\n                            <\/pre>\r\n                        <\/div>\r\n                    <\/div>\r\n                                    <\/div>\r\n    <\/div>\r\n\n<p class=\"wp-block-paragraph\">Further information on sending much more complicated emails can be found in the official\u00a0<a href=\"http:\/\/swiftmailer.org\/docs\/messages.html\" target=\"_tabs\" rel=\"noopener noreferrer\">Swiftmailer documentation available online over here<\/a>.<\/p>\n\n<h2 class=\"wp-block-heading\">Using PHP Mailer<\/h2>\n\n<p class=\"wp-block-paragraph\">Mailgun can also be used in conjunction with one of the most famous PHP emailing libraries:\u00a0<a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\" target=\"_tabs\" rel=\"noopener noreferrer\">PHP Mailer<\/a>.<\/p>\n\n<p class=\"wp-block-paragraph\">PHP Mailer is a very valid alternative to the in-built php\u00a0<code>mail()<\/code>\u00a0function, as it speeds up and allows you to heavily customise the way you send emails with PHP. Linux distributions and Mac OS X have <b>an in-built function<\/b> to send emails although unreliable. By the way, it is worth mentioning that Windows hasn\u2019t got anything at all to send emails, thus I suggest you keep reading.<\/p>\n\n<h3 class=\"wp-block-heading\">Steps<\/h3>\n\n<ul class=\"wp-block-list\">\n<li>Create a folder called <code>phpmailer<\/code>\n<ul class=\"wp-block-list\">\n<li><code>mkdir phpmailer<\/code><\/li>\n\n\n\n<li><code>cd phpmailer<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Download composer to install <code>phpmailer <\/code>and its dependencies\n<ul class=\"wp-block-list\">\n<li><code>curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Composer is ready, let\u2019s install Phpmailer(5.2.8) now:\n<ul class=\"wp-block-list\">\n<li><code>php composer.phar require phpmailer\/phpmailer:5.2.8<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Files will be downloaded, once finished simply create a your own empty file to test that phpmailer works\n<ul class=\"wp-block-list\">\n<li><code>touch mailgun_with_phpmailer.php<\/code><\/li>\n\n\n\n<li><code>vim mailgun_with_phpmailer.php<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n<p class=\"wp-block-paragraph\">Here we\u2019re using vim, but you can edit the file\u00a0<code>mailgun_with_phpmailer.php<\/code>\u00a0in whichever editor you feel most comfortable with.<\/p>\n    <div data-content-type=\"longform\" class=\"code-snippet longform-spacings rounded-lg overflow-hidden shadow \" data-count=\"1\">\r\n        <ul class=\"nav nav-buttons code-snippet__tabs longform-except position-relative d-flex gap-2 flex-wrap p-3\" role=\"tablist\">\r\n\r\n                            \r\n                    \r\n                        <li class=\"nav-item\" role=\"presentation\">\r\n                            <button class=\"nav-link active\" data-bs-toggle=\"tab\" data-bs-target=\"#code_0\" type=\"button\" role=\"tab\" aria-controls=\"code_0\" aria-selected=\"true\">\r\n                                PHP                            <\/button>\r\n                        <\/li>\r\n\r\n                    \r\n                            \r\n        <\/ul>\r\n        <div class=\"code-snippet__tab-content tab-content\">\r\n\r\n                            \r\n                    <div class=\"tab-pane show active\" id=\"code_0\" role=\"tabpanel\" aria-labelledby=\"code_0\">\r\n                        <div class=\"code-snippet__code-wrapper\">\r\n                            <div class=\"nav nav-buttons code-snippet__copy-wrapper\">\r\n                                <button type=\"button\" class=\"nav-link code-snippet__copy\" data-clipboard-target=\"#code_0_content\" data-clipboard-label=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-clipboard-success-label=\"Copiado!\" data-clipboard-error-label=\"N\u00e3o foi poss\u00edvel copiar\" data-tooltip=\"Copiar para a \u00e1rea de transfer\u00eancia\" data-tooltip-placement=\"left\" role=\"tooltip\" aria-label=\"Copiar para a \u00e1rea de transfer\u00eancia\">\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--default\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/file-stack.svg\"><\/svg>\r\n                                    <svg class=\"code-snippet__copy-icon code-snippet__copy-icon--success\" width=\"24\" height=\"24\" aria-hidden=\"true\" data-url=\"https:\/\/www.mailgun.com\/wp-content\/plugins\/sinch-core\/assets\/icons\/sinch\/badge-check.svg\"><\/svg>\r\n                                <\/button>\r\n                            <\/div>\r\n                            <pre class=\"w-100 h-100 m-0 line-numbers language-PHP\">\r\n                                <code id=\"code_0_content\" class=\"language-PHP\">\r\n\r\n                                    isSMTP();  \/\/ Set mailer to use SMTP\r\n$mail-&gt;Host = &#039;smtp.mailgun.org&#039;;  \/\/ Specify mailgun SMTP servers\r\n$mail-&gt;SMTPAuth = true; \/\/ Enable SMTP authentication\r\n$mail-&gt;Username = &#039;username@domain.com&#039;; \/\/ SMTP username from https:\/\/mailgun.com\/cp\/domains\r\n$mail-&gt;Password = &#039;myp@55w0rd&#039;; \/\/ SMTP password from https:\/\/mailgun.com\/cp\/domains\r\n$mail-&gt;SMTPSecure = &#039;tls&#039;;   \/\/ Enable encryption, &#039;ssl&#039;\r\n\r\n$mail-&gt;From = &#039;sender@domain.com&#039;; \/\/ The FROM field, the address sending the email \r\n$mail-&gt;FromName = &#039;Orlie&#039;; \/\/ The NAME field which will be displayed on arrival by the email client\r\n$mail-&gt;addAddress(&#039;recipient@domain.net&#039;, &#039;BOB&#039;);     \/\/ Recipient&#039;s email address and optionally a name to identify him\r\n$mail-&gt;isHTML(true);   \/\/ Set email to be sent as HTML, if you are planning on sending plain text email just set it to false\r\n\r\n\/\/ The following is self explanatory\r\n$mail-&gt;Subject = &#039;Email sent with Mailgun&#039;;\r\n$mail-&gt;Body    = &#039;&lt;span style=&quot;color: red&quot;&gt;Mailgun rocks&lt;\/span&gt;, thank you &lt;em&gt;phpmailer&lt;\/em&gt; for making emailing easy using this &lt;b&gt;tool!&lt;\/b&gt;&#039;;\r\n$mail-&gt;AltBody = &#039;Mailgun rocks, shame you can&#039;t see the html sent with phpmailer so you&#039;re seeing this instead&#039;;\r\n\r\nif(!$mail-&gt;send()) {  \r\n    echo &quot;Message hasn&#039;t been sent.&quot;;\r\n    echo &#039;Mailer Error: &#039; . $mail-&gt;ErrorInfo . &quot;n&quot;;\r\n} else {\r\n    echo &quot;Message has been sent  n&quot;;\r\n\r\n}\r\n?&gt;\r\n                                <\/code>\r\n                            <\/pre>\r\n                        <\/div>\r\n                    <\/div>\r\n                                    <\/div>\r\n    <\/div>\r\n\n<p class=\"wp-block-paragraph\">The settings for authenticating with\u00a0<a href=\"https:\/\/help.mailgun.com\/hc\/en-us\/articles\/203380100-Where-can-I-find-my-API-key-and-SMTP-credentials-\" target=\"_tabs\" rel=\"noopener noreferrer\">Mailgun\u2019s SMTP server can be found over here<\/a>, just like with Swiftmailer.<\/p>\n\n<p class=\"wp-block-paragraph\">Log into your Mailgun control panel, pick a domain (or the sandbox domain that comes with every fresh account if you have not configured a custom domain), and copy paste these details (Default SMTP Login, Default Password) in the code snippet above in the smtp username and password fields.<\/p>\n\n<h2 class=\"wp-block-heading\">Baking is awesome<\/h2>\n\n<p class=\"wp-block-paragraph\">Do you use other libraries in conjunction with Mailgun? If so we\u2019d be happy to feature it in our next PHP blog post with full coverage and examples!<\/p>\n\n<p class=\"wp-block-paragraph\">Happy Sending!<\/p>\n\r\n    <aside data-content-type=\"longform\"  class=\"banner-block longform-spacings rounded-lg shadow-lg px-5 py-6 px-md-6 py-md-7 p-lg-7 bg-light\" data-theme=\"dark\" aria-labelledby=\"banner-block-block_10767e75cc69ec0631aea40ec931487f\">\r\n                    <p class=\"text-uppercase section-caption text-body-color\">\r\n                Sign Up            <\/p>\r\n                            <p class=\"h4 text-accent fw-bold\" id=\"banner-block-block_10767e75cc69ec0631aea40ec931487f\">\r\n                It&#8217;s easy to get started. And it&#8217;s free.            <\/p>\r\n                            <div class=\"mb-0 text-body-color\">\r\n                                    <div class=\"content-body mb-0 text-body-color\">See what you can accomplish with the world\u2019s best email delivery platform.<\/div>\r\n                            <\/div>\r\n        \r\n        <div class=\"text-start mt-5\"><a href=\"https:\/\/signup.mailgun.com\/new\/signup\"  class=\"btn btn-secondary\" >Get started<\/a><\/div>\r\n    <\/aside>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Quando comecei na programa\u00e7\u00e3o, lembro de debater se queria aprender ASP.NET ou PHP. Li milhares de f\u00f3runs e t\u00f3picos sobre cada linguagem para me ajudar a tomar uma decis\u00e3o.<\/p>\n","protected":false},"author":34,"featured_media":4074,"menu_order":0,"template":"","meta":{"_acf_changed":false,"footnotes":""},"blog_category":[20],"class_list":["post-20540","blog","type-blog","status-publish","has-post-thumbnail","hentry","blog_category-email"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun<\/title>\n<meta name=\"description\" content=\"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun\" \/>\n<meta property=\"og:description\" content=\"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/\" \/>\n<meta property=\"og:site_name\" content=\"Transactional Email API Service For Developers | Mailgun\" \/>\n<meta property=\"article:modified_time\" content=\"2026-09-15T03:52:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/07\/MG-Blog-Email.png\" \/>\n\t<meta property=\"og:image:width\" content=\"720\" \/>\n\t<meta property=\"og:image:height\" content=\"448\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/\",\"url\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/\",\"name\":\"Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mailgun.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/MG-Blog-Email.png\",\"datePublished\":\"2022-09-26T00:00:00+00:00\",\"dateModified\":\"2026-09-15T03:52:50+00:00\",\"description\":\"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mailgun.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/MG-Blog-Email.png\",\"contentUrl\":\"https:\\\/\\\/www.mailgun.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/MG-Blog-Email.png\",\"width\":720,\"height\":448},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/email\\\/php-como-usar-mailgun-eficiencia\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/inicio\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Como preparar o Mailgun rapidamente com PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#website\",\"url\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/\",\"name\":\"Transactional Email API Service For Developers | Mailgun\",\"description\":\"Poderosas APIs de e-mail transacional que permitem a voc\u00ea enviar, receber e rastrear e-mails, criadas com os desenvolvedores em mente. Saiba mais hoje mesmo!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#organization\",\"name\":\"Transactional Email API Service For Developers | Mailgun\",\"url\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mailgun.com\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-android-chrome-512x512-1.png\",\"contentUrl\":\"https:\\\/\\\/www.mailgun.com\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-android-chrome-512x512-1.png\",\"width\":512,\"height\":512,\"caption\":\"Transactional Email API Service For Developers | Mailgun\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mailgun.com\\\/pt\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun","description":"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.","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:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/","og_locale":"pt_BR","og_type":"article","og_title":"Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun","og_description":"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.","og_url":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/","og_site_name":"Transactional Email API Service For Developers | Mailgun","article_modified_time":"2026-09-15T03:52:50+00:00","og_image":[{"width":720,"height":448,"url":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/07\/MG-Blog-Email.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/","url":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/","name":"Como preparar o Mailgun rapidamente com PHP - Transactional Email API Service For Developers | Mailgun","isPartOf":{"@id":"https:\/\/www.mailgun.com\/pt\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/#primaryimage"},"image":{"@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/07\/MG-Blog-Email.png","datePublished":"2022-09-26T00:00:00+00:00","dateModified":"2026-09-15T03:52:50+00:00","description":"Usar o Mailgun no seu ambiente PHP \u00e9 f\u00e1cil, mas h\u00e1 muito material desatualizado por a\u00ed sobre o PHP. Felizmente, estes exemplos simples explicam o que fazer.","breadcrumb":{"@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/#primaryimage","url":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/07\/MG-Blog-Email.png","contentUrl":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/07\/MG-Blog-Email.png","width":720,"height":448},{"@type":"BreadcrumbList","@id":"https:\/\/www.mailgun.com\/pt\/blog\/email\/php-como-usar-mailgun-eficiencia\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mailgun.com\/pt\/inicio\/"},{"@type":"ListItem","position":2,"name":"Blog","item":"https:\/\/www.mailgun.com\/pt\/blog\/"},{"@type":"ListItem","position":3,"name":"Como preparar o Mailgun rapidamente com PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.mailgun.com\/pt\/#website","url":"https:\/\/www.mailgun.com\/pt\/","name":"Transactional Email API Service For Developers | Mailgun","description":"Poderosas APIs de e-mail transacional que permitem a voc\u00ea enviar, receber e rastrear e-mails, criadas com os desenvolvedores em mente. Saiba mais hoje mesmo!","publisher":{"@id":"https:\/\/www.mailgun.com\/pt\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mailgun.com\/pt\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Organization","@id":"https:\/\/www.mailgun.com\/pt\/#organization","name":"Transactional Email API Service For Developers | Mailgun","url":"https:\/\/www.mailgun.com\/pt\/","logo":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.mailgun.com\/pt\/#\/schema\/logo\/image\/","url":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/06\/cropped-android-chrome-512x512-1.png","contentUrl":"https:\/\/www.mailgun.com\/wp-content\/uploads\/2025\/06\/cropped-android-chrome-512x512-1.png","width":512,"height":512,"caption":"Transactional Email API Service For Developers | Mailgun"},"image":{"@id":"https:\/\/www.mailgun.com\/pt\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/blog\/20540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/users\/34"}],"version-history":[{"count":2,"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/blog\/20540\/revisions"}],"predecessor-version":[{"id":20656,"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/blog\/20540\/revisions\/20656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/media\/4074"}],"wp:attachment":[{"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/media?parent=20540"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.mailgun.com\/pt\/wp-json\/wp\/v2\/blog_category?post=20540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}