{"id":23201,"date":"2020-12-09T10:04:08","date_gmt":"2020-12-09T02:04:08","guid":{"rendered":"https:\/\/web.mwwsb.com.my\/pjci\/?post_type=kb&#038;p=23201"},"modified":"2022-09-08T20:00:25","modified_gmt":"2022-09-08T12:00:25","slug":"how-to-sending-email-using-php-phpmailer","status":"publish","type":"kb","link":"https:\/\/www.casbay.com\/guide\/kb\/how-to-sending-email-using-php-phpmailer","title":{"rendered":"Sending email using PHP (PHPMailer)"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"23201\" class=\"elementor elementor-23201\" data-elementor-post-type=\"kb\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-adcb517 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"adcb517\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ffbfca5\" data-id=\"ffbfca5\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-17e8386 elementor-widget elementor-widget-heading\" data-id=\"17e8386\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Sending email using PHP (PHPMailer)<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7a71c1f elementor-widget elementor-widget-text-editor\" data-id=\"7a71c1f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>With SMTP authentication, you can send an email via PHP. You just have to download PHPMailer class files to use the PHPMailer courses:<\/p><p>The following is the sample code for SMTP authentication to send e-mails using the PHP script:<\/p><p>$mail-&gt;Host = \u201cmail.domain.com\u201d; \/\/ SMTP servers<br \/>$mail-&gt;SMTPSecure = \u2018ssl\u2019; \/\/ Enable SSL encryption, then change below port to 587.<\/p><p><strong>You may\u00a0remove this line to use non-SSL with port 25 or 26 in below.<\/strong><br \/>$mail-&gt;Port = \u201c25\u201d; \/\/specify SMTP Port<br \/>$mail-&gt;SMTPAuth = true; \/\/ turn on SMTP authentication<br \/>$mail-&gt;Username = \u201cemail@domain.com\u201d; \/\/ SMTP username<br \/>$mail-&gt;Password = \u201cpassword\u201d; \/\/ SMTP password<\/p><p>$mail-&gt;From\u00a0\u00a0\u00a0\u00a0 = \u201cemail@domain.com\u201d;<br \/>$mail-&gt;FromName = \u201cName\u201d;<br \/>$mail-&gt;AddAddress(\u201cRecipient@emailaddress.com\u201d,\u201dName\u201d);<br \/>$mail-&gt;AddReplyTo(\u201cyourname@domain.com\u201d,\u201dYour Name\u201d);<\/p><p>$mail-&gt;WordWrap = 50;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ set word wrap<\/p><p>$mail-&gt;IsHTML(true);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ send as HTML<\/p><p>$mail-&gt;Subject\u00a0 =\u00a0 \u201cHere is the subject\u201d;<br \/>$mail-&gt;Body\u00a0\u00a0\u00a0\u00a0 =\u00a0 \u201cThis is the\u00a0<b>HTML body<\/b>\u201c;<br \/>$mail-&gt;AltBody\u00a0 =\u00a0 \u201cThis is the text-only body\u201d;<\/p><p>if(!$mail-&gt;Send())<br \/>{<br \/>echo \u201cMessage was not sent<\/p><p>\u201c;<br \/>echo \u201cMailer Error: \u201d . $mail-&gt;ErrorInfo;<br \/>exit;<br \/>}<\/p><p>echo \u201cMessage has been sent\u201d;<\/p><p>?&gt;<\/p><p>Please ensure that you make needed modifications to the above script.<\/p><p><strong>Note:<\/strong> Our mail servers require your SMTP authentication script to submit an email for safety reasons.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8c8a7f3 elementor-widget elementor-widget-heading\" data-id=\"8c8a7f3\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-medium\">Let's Look into PHPMailer<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c15003f elementor-widget elementor-widget-text-editor\" data-id=\"c15003f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>PHPMailer is one of the most popular open-source PHP libraries to send emails with. It was first released back in 2001, and since then it has become a PHP developer\u2019s favorite way of sending emails programmatically, aside from others such as <a href=\"https:\/\/www.sitepoint.com\/sending-email-with-swift-mailer\/\" class=\"broken_link\" rel=\"noopener\">Swiftmailer<\/a>.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9cd69d0 elementor-widget elementor-widget-heading\" data-id=\"9cd69d0\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-medium\">Why Do We Need It?<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6391295 elementor-widget elementor-widget-text-editor\" data-id=\"6391295\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>First and foremost, there are PHP developers who need to send email from their code. The only PHP function that can make this work directly is <a href=\"https:\/\/www.php.net\/manual\/en\/function.mail.php\" rel=\"nofollow noopener\"><code>mail()<\/code><\/a>. However, it does not provide assistance for making use of popular features like HTML messages, encryption, authentication, and attachments.<\/p><p>Besides, formatting email correctly is surprisingly difficult. There are myriad overlapping and conflicting standards. They require tight adherence to horribly complicated formatting and encoding rules. The vast majority of code that you&#8217;ll find online that uses the\u00a0<code>mail()<\/code>\u00a0 function directly is just plain wrong, if not unsafe!<\/p><p>Furthermore, the PHP <code>mail()<\/code>\u00a0function usually sends via a local mail server, typically fronted by a\u00a0<code>sendmail<\/code> binary on Linux, BSD, and macOS platforms, however, Windows. It usually doesn&#8217;t include a local mail server. Moreover, PHPMailer&#8217;s integrated SMTP client allows email sending on all platforms without even needing a local mail server. However, please be aware, we should avoid the\u00a0<code>mail()<\/code>function when possible; it&#8217;s both faster and <a href=\"https:\/\/exploitbox.io\/paper\/Pwning-PHP-Mail-Function-For-Fun-And-RCE.html\" rel=\"nofollow noopener\">safer<\/a>\u00a0to use SMTP to localhost.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-394cdc5 elementor-widget elementor-widget-text-editor\" data-id=\"394cdc5\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>Here is a suggestion:<br \/>Please don&#8217;t be tempted to do it yourself. If you don&#8217;t use PHPMailer, there are many other excellent libraries out there existed that you should look at before rolling your own. Try <a href=\"https:\/\/github.com\/zetacomponents\/Mail\" rel=\"noopener\">ZetaComponents<\/a>, <a href=\"https:\/\/swiftmailer.symfony.com\/\" rel=\"nofollow noopener\">SwiftMailer<\/a>\u00a0,\u00a0<a href=\"https:\/\/docs.laminas.dev\/laminas-mail\/\" rel=\"nofollow noopener\">Laminas\/Mail<\/a>, \u00a0etc.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Sending email using PHP (PHPMailer) With SMTP authentication, you can send an email via PHP. You just have to download PHPMailer class files to use the PHPMailer courses: The following is the sample code for SMTP authentication to send e-mails using the PHP script: $mail-&gt;Host = \u201cmail.domain.com\u201d; \/\/ SMTP servers $mail-&gt;SMTPSecure = \u2018ssl\u2019; \/\/ Enable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"no-sidebar","site-content-layout":"page-builder","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}}},"kbtopic":[113],"kbtag":[106],"mkb_version":[],"_links":{"self":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/23201"}],"collection":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/comments?post=23201"}],"version-history":[{"count":1,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/23201\/revisions"}],"predecessor-version":[{"id":31690,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/23201\/revisions\/31690"}],"wp:attachment":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/media?parent=23201"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=23201"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtag?post=23201"},{"taxonomy":"mkb_version","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/mkb_version?post=23201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}