{"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&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
With SMTP authentication, you can send an email via PHP. You just have to download PHPMailer class files to use the PHPMailer courses:<\/p>
The following is the sample code for SMTP authentication to send e-mails using the PHP script:<\/p>
$mail->Host = \u201cmail.domain.com\u201d; \/\/ SMTP servers
$mail->SMTPSecure = \u2018ssl\u2019; \/\/ Enable SSL encryption, then change below port to 587.<\/p>
You may\u00a0remove this line to use non-SSL with port 25 or 26 in below.<\/strong> $mail->From\u00a0\u00a0\u00a0\u00a0 = \u201cemail@domain.com\u201d; $mail->WordWrap = 50;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ set word wrap<\/p> $mail->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> $mail->Subject\u00a0 =\u00a0 \u201cHere is the subject\u201d; if(!$mail->Send()) \u201c; echo \u201cMessage has been sent\u201d;<\/p> ?><\/p> Please ensure that you make needed modifications to the above script.<\/p> 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 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 Swiftmailer<\/a>.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t 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 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’ll find online that uses the\u00a0 Furthermore, the PHP Here is a suggestion: 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->Host = \u201cmail.domain.com\u201d; \/\/ SMTP servers $mail->SMTPSecure = \u2018ssl\u2019; \/\/ Enable […]<\/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}]}}
$mail->Port = \u201c25\u201d; \/\/specify SMTP Port
$mail->SMTPAuth = true; \/\/ turn on SMTP authentication
$mail->Username = \u201cemail@domain.com\u201d; \/\/ SMTP username
$mail->Password = \u201cpassword\u201d; \/\/ SMTP password<\/p>
$mail->FromName = \u201cName\u201d;
$mail->AddAddress(\u201cRecipient@emailaddress.com\u201d,\u201dName\u201d);
$mail->AddReplyTo(\u201cyourname@domain.com\u201d,\u201dYour Name\u201d);<\/p>
$mail->Body\u00a0\u00a0\u00a0\u00a0 =\u00a0 \u201cThis is the\u00a0HTML body<\/b>\u201c;
$mail->AltBody\u00a0 =\u00a0 \u201cThis is the text-only body\u201d;<\/p>
{
echo \u201cMessage was not sent<\/p>
echo \u201cMailer Error: \u201d . $mail->ErrorInfo;
exit;
}<\/p>Let's Look into PHPMailer<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
Why Do We Need It?<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
mail()<\/code><\/a>. However, it does not provide assistance for making use of popular features like HTML messages, encryption, authentication, and attachments.<\/p>mail()<\/code>\u00a0 function directly is just plain wrong, if not unsafe!<\/p>mail()<\/code>\u00a0function usually sends via a local mail server, typically fronted by a\u00a0sendmail<\/code> binary on Linux, BSD, and macOS platforms, however, Windows. It usually doesn’t include a local mail server. Moreover, PHPMailer’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\u00a0mail()<\/code>function when possible; it’s both faster and 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
Please don’t be tempted to do it yourself. If you don’t use PHPMailer, there are many other excellent libraries out there existed that you should look at before rolling your own. Try ZetaComponents<\/a>, SwiftMailer<\/a>\u00a0,\u00a0Laminas\/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":"