{"id":2318,"date":"2017-04-13T17:08:47","date_gmt":"2017-04-13T17:08:47","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=2318"},"modified":"2024-01-18T13:10:22","modified_gmt":"2024-01-18T13:10:22","slug":"send-html-email-php-gmail-smtp-phpmailer","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/","title":{"rendered":"Send Email via SMTP Server in PHP using PHPMailer"},"content":{"rendered":"<p>Send email from the script is the most used functionality in the web application. The <b>PHP mail()<\/b> function is used to <a href=\"https:\/\/www.codexworld.com\/send-beautiful-html-email-using-php\/\">send emails from the PHP script<\/a>. When you send an email using the mail() function in PHP, the mail is sent from your web server. Sometimes it may cause issues in sending an email and fails to deliver mail to the recipient. With SMTP you can overcome this issue, SMTP is the most recommended way to send email from the PHP script. When you send an email via SMTP, the email is sent from the mail server rather than the web hosting server.<\/p>\n<p>The easiest way to <b>send email in PHP with SMTP<\/b> is to use the PHPMailer library. PHPMailer provides the ability to send an email via an SMTP server using PHP. Various configuration options of the PHPMailer library allow you to configure and customize the email sending functionality as per your needs. You can send a text or HTML email with single or multiple attachments using PHPMailer. In this tutorial, we will show you how to <b>send HTML email with SMTP in PHP using PHPMailer<\/b>.<\/p>\n<p>In the example script, we will integrate PHPMailer in PHP and <b>send SMTP mail using PHPMailer<\/b> library. Use the following example code to send HTML email with attachment using PHP.<\/p>\n<h2>Send HTML Email via SMTP Server<\/h2>\n<p><b>PHPMailer Library:<\/b><br \/>\nWe will use PHPMailer to send emails via SMTP server. So, include the PHPMailer library files and initialize the PHPMailer object.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Import&nbsp;PHPMailer&nbsp;classes&nbsp;into&nbsp;the&nbsp;global&nbsp;namespace <br \/><\/span><span style=\"color: #007700\">use&nbsp;<\/span><span style=\"color: #0000BB\">PHPMailer\\PHPMailer\\PHPMailer<\/span><span style=\"color: #007700\">; <br \/>use&nbsp;<\/span><span style=\"color: #0000BB\">PHPMailer\\PHPMailer\\SMTP<\/span><span style=\"color: #007700\">; <br \/>use&nbsp;<\/span><span style=\"color: #0000BB\">PHPMailer\\PHPMailer\\Exception<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;PHPMailer&nbsp;library&nbsp;files <br \/><\/span><span style=\"color: #007700\">require&nbsp;<\/span><span style=\"color: #DD0000\">'PHPMailer\/Exception.php'<\/span><span style=\"color: #007700\">; <br \/>require&nbsp;<\/span><span style=\"color: #DD0000\">'PHPMailer\/PHPMailer.php'<\/span><span style=\"color: #007700\">; <br \/>require&nbsp;<\/span><span style=\"color: #DD0000\">'PHPMailer\/SMTP.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;an&nbsp;instance&nbsp;of&nbsp;PHPMailer&nbsp;class <br \/><\/span><span style=\"color: #0000BB\">$mail&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">PHPMailer<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p><span class=\"note\">Note that:<\/span> You don&#8217;t need to download the PHPMailer library separately. All the required PHPMailer library files are included in the source code and you can install PHPMailer without composer in PHP.<\/p>\n<p><b>SMTP Configuration:<\/b><br \/>\nSpecify the SMTP server host (<code>$mail->Host<\/code>), username (<code>$mail->Username<\/code>), password (<code>$mail->Password<\/code>), encryption type (<code>$mail->SMTPSecure<\/code>), and port (<code>$mail->Port<\/code>) as per your SMTP server credentials.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;SMTP&nbsp;configuration<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isSMTP<\/span><span style=\"color: #007700\">();<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Host&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'smtp.example.com'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">SMTPAuth&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Username&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'user@example.com'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Password&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'******'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">SMTPSecure&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'tls'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Port&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">587<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p><b>Configure Email:<\/b><br \/>\nSpecify some basic email settings (like, sender email &#038; name, recipient email, subject, message, etc). Set <code>isHTML()<\/code> to TRUE for sending email in HTML format.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Sender&nbsp;info <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">setFrom<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'sender@example.com'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'SenderName'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addReplyTo<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'reply@example.com'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'SenderName'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;a&nbsp;recipient <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAddress<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'recipient@example.com'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;cc&nbsp;or&nbsp;bcc&nbsp; <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addCC<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'cc@example.com'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addBCC<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'bcc@example.com'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Email&nbsp;subject <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Subject&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Send&nbsp;Email&nbsp;via&nbsp;SMTP&nbsp;using&nbsp;PHPMailer'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;email&nbsp;format&nbsp;to&nbsp;HTML <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isHTML<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Email&nbsp;body&nbsp;content <br \/><\/span><span style=\"color: #0000BB\">$mailContent&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">' <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Send&nbsp;HTML&nbsp;Email&nbsp;using&nbsp;SMTP&nbsp;Server&nbsp;in&nbsp;PHP&lt;\/h2&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;It&nbsp;is&nbsp;a&nbsp;test&nbsp;email&nbsp;by&nbsp;CodexWorld,&nbsp;sent&nbsp;via&nbsp;SMTP&nbsp;server&nbsp;with&nbsp;PHPMailer&nbsp;using&nbsp;PHP.&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Read&nbsp;the&nbsp;tutorial&nbsp;and&nbsp;download&nbsp;this&nbsp;script&nbsp;from&nbsp;&lt;a&nbsp;href=\"https:\/\/www.codexworld.com\/\"&gt;CodexWorld&lt;\/a&gt;.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Body&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mailContent<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Send&nbsp;email <br \/><\/span><span style=\"color: #007700\">if(!<\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">send<\/span><span style=\"color: #007700\">()){ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Message&nbsp;could&nbsp;not&nbsp;be&nbsp;sent.&nbsp;Mailer&nbsp;Error:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">ErrorInfo<\/span><span style=\"color: #007700\">; <br \/>}else{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Message&nbsp;has&nbsp;been&nbsp;sent.'<\/span><span style=\"color: #007700\">; <br \/>}<\/span><\/pre>\n<h2>Send HTML Email with Attachments<\/h2>\n<p>Use the <b>addAttachment()<\/b> method of PHPMailer class to add an attachment to the email. You can attach multiple attachments to the email by adding <code>addAttachment()<\/code> method multiple times.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;attachments<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAttachment<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'files\/codexworld.pdf'<\/span><span style=\"color: #007700\">);<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAttachment<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'files\/codexworld.docx'<\/span><span style=\"color: #007700\">);<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAttachment<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'images\/codexworld.png'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'new-name.png'<\/span><span style=\"color: #007700\">);&nbsp;<\/span><span style=\"color: #FF8000\">\/\/set&nbsp;new&nbsp;name<\/span><\/pre>\n<h2>Send Email to Multiple Recipients<\/h2>\n<p>Add <b>addAddress()<\/b> method multiple times for sending email to multiple recipients.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;multiple&nbsp;recipients<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAddress<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'john.doe@gmail.com'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'John&nbsp;Doe'<\/span><span style=\"color: #007700\">);<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">addAddress<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'mark@example.com'<\/span><span style=\"color: #007700\">);&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;name&nbsp;is&nbsp;optional<\/span><\/pre>\n<p class=\"seeAlso\"><span><\/span><a href=\"https:\/\/www.codexworld.com\/codeigniter-send-email-using-phpmailer-gmail-smtp\/\">Send Email using PHPMailer in CodeIgniter<\/a><\/span><\/p>\n<h2>Send Email using Gmail SMTP<\/h2>\n<p>If you want to use Gmail SMTP to send email, you need to make some changes in Google account settings. Follow the below steps to use Gmail SMTP in PHPMailer library.<\/p>\n<ul class=\"step_list\">\n<li>Login to your Google account. Go to the <a href=\"https:\/\/myaccount.google.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">My Account page<\/a>.<\/li>\n<li>Navigate to the <b>Security<\/b> page and scroll down to the <b>Signing in to Google<\/b> section &raquo; Turn <b>Off<\/b> the <\/b>2-Step Verification<\/li>\n<p>.<\/p>\n<div class=\"img_center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-1024x378.png\" alt=\"google-account-security-2-step-verification-gmail-smtp-codexworld\" width=\"960\" height=\"354\" class=\"alignnone size-large wp-image-4542\" srcset=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-1024x378.png 1024w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-300x111.png 300w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-768x284.png 768w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-200x74.png 200w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld-346x128.png 346w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-2-step-verification-gmail-smtp-codexworld.png 1529w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/div>\n<\/li>\n<li>Scroll down the <b>Less secure app access<\/b> section and turn <b>On<\/b> Less secure app access.\n<div class=\"img_center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-1024x333.png\" alt=\"google-account-security-less-secure-app-access-gmail-smtp-codexworld\" width=\"960\" height=\"312\" class=\"alignnone size-large wp-image-4543\" srcset=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-1024x333.png 1024w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-300x97.png 300w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-768x249.png 768w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-200x65.png 200w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld-346x112.png 346w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/google-account-security-less-secure-app-access-gmail-smtp-codexworld.png 1490w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/div>\n<\/li>\n<\/li>\n<\/ul>\n<p>You are done! Now you can use Gmail SMTP to send email from the PHP script.<\/p>\n<p>Specify your Gmail account credentials (email address and password), SMTP host, and port to send email using Gmail SMTP.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;SMTP&nbsp;configuration<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isSMTP<\/span><span style=\"color: #007700\">();<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Host&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'smtp.gmail.com'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">SMTPAuth&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Username&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'codexworld@gmail.com'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Password&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'********'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">SMTPSecure&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'tls'<\/span><span style=\"color: #007700\">;<br \/><\/span><span style=\"color: #0000BB\">$mail<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Port&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">587<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p class=\"seeAlso\"><span><\/span><a href=\"https:\/\/www.codexworld.com\/how-to-send-email-from-localhost-in-php\/\">Send Email from Localhost in PHP<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Send email from the script is the most used functionality in the web application. The PHP mail() function is used to send emails from the PHP script. When you send an email using the mail() <\/p>\n","protected":false},"author":1,"featured_media":5556,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[23,277,24,14,147,276],"class_list":["post-2318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-email","tag-gmail","tag-html-email","tag-php","tag-phpmailer","tag-smtp","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Send Email via SMTP Server in PHP using PHPMailer - CodexWorld<\/title>\n<meta name=\"description\" content=\"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.\" \/>\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.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Send Email via SMTP Server in PHP using PHPMailer - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-13T17:08:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-18T13:10:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Send Email via SMTP Server in PHP using PHPMailer\",\"datePublished\":\"2017-04-13T17:08:47+00:00\",\"dateModified\":\"2024-01-18T13:10:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/\"},\"wordCount\":511,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png\",\"keywords\":[\"Email\",\"Gmail\",\"HTML Email\",\"PHP\",\"PHPMailer\",\"SMTP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/\",\"name\":\"Send Email via SMTP Server in PHP using PHPMailer - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png\",\"datePublished\":\"2017-04-13T17:08:47+00:00\",\"dateModified\":\"2024-01-18T13:10:22+00:00\",\"description\":\"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png\",\"width\":1920,\"height\":1080,\"caption\":\"send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-html-email-php-gmail-smtp-phpmailer\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Send Email via SMTP Server in PHP using PHPMailer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Send Email via SMTP Server in PHP using PHPMailer - CodexWorld","description":"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.","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.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/","og_locale":"en_US","og_type":"article","og_title":"Send Email via SMTP Server in PHP using PHPMailer - CodexWorld","og_description":"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.","og_url":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2017-04-13T17:08:47+00:00","article_modified_time":"2024-01-18T13:10:22+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Send Email via SMTP Server in PHP using PHPMailer","datePublished":"2017-04-13T17:08:47+00:00","dateModified":"2024-01-18T13:10:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/"},"wordCount":511,"commentCount":9,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","keywords":["Email","Gmail","HTML Email","PHP","PHPMailer","SMTP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/","url":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/","name":"Send Email via SMTP Server in PHP using PHPMailer - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","datePublished":"2017-04-13T17:08:47+00:00","dateModified":"2024-01-18T13:10:22+00:00","description":"Send Email via SMTP Server in PHP - Use PHPMailer to send email with SMTP from PHP script. Example script to send HTML email with multiple attachments using Gmail SMTP in PHP.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","width":1920,"height":1080,"caption":"send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Send Email via SMTP Server in PHP using PHPMailer"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/04\/send-html-email-with-attachment-via-smtp-using-php-phpmailer-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-Bo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":11,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":5554,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2318\/revisions\/5554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/5556"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}