{"id":753,"date":"2014-11-05T17:28:24","date_gmt":"2014-11-05T11:58:24","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=753"},"modified":"2023-07-06T19:28:09","modified_gmt":"2023-07-06T13:58:09","slug":"phpmailer-ultimate-tutorial","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/phpmailer-ultimate-tutorial\/","title":{"rendered":"PHPMailer ultimate tutorial"},"content":{"rendered":"<p>PHP have inbuilt <strong>mail()\u00a0<\/strong>function to send email right from the code. This is easy to use and effective too but it has one serious problem. It gives load to production server and also provide no guarantee about email delivery.<\/p>\n<p><strong>PHPMailer\u00a0<\/strong>is one of the popular and easy to use email library for <a href=\"https:\/\/codeforgeek.com\/php\/\" title=\"PHP tutorial\" target=\"_blank\" rel=\"noopener\">php<\/a>. This solves the issue of production load and guarantee of email delivery by allowing us to configure <a title=\"SMTP wiki page\" href=\"https:\/\/en.wikipedia.org\/wiki\/Simple_Mail_Transfer_Protocol\" target=\"_blank\" rel=\"noopener\">SMTP <\/a>server.<\/p>\n<h2>How to use it?<\/h2>\n<p>For your ease i have created a custom function which of course uses PHPMailer and SMTP. Here is a function.<br \/>\n<code lang=\"php\"><br \/>\n<?php\n    require_once('class.phpmailer.php');\n    function sendmail($to,$subject,$message,$name)\n    {\n                  $mail             = new PHPMailer();\n                  $body             = $message;\n                  $mail->IsSMTP();<br \/>\n                  $mail->SMTPAuth   = true;<br \/>\n                  $mail->Host       = \"smtp.gmail.com\";<br \/>\n                  $mail->Port       = 587;<br \/>\n                  $mail->Username   = \"youraccount@gmail.com\";<br \/>\n                  $mail->Password   = \"your gmail password\";<br \/>\n                  $mail->SMTPSecure = 'tls';<br \/>\n                  $mail->SetFrom('youraccount@gmail.com', 'Your name');<br \/>\n                  $mail->AddReplyTo(\"youraccount@gmail.com\",\"Your name\");<br \/>\n                  $mail->Subject    = $subject;<br \/>\n                  $mail->AltBody    = \"Any message.\";<br \/>\n                  $mail->MsgHTML($body);<br \/>\n                  $address = $to;<br \/>\n                  $mail->AddAddress($address, $name);<br \/>\n                  if(!$mail->Send()) {<br \/>\n                      return 0;<br \/>\n                  } else {<br \/>\n                        return 1;<br \/>\n                 }<br \/>\n    }<br \/>\n?><br \/>\n<\/code><br \/>\nTo use this you need two files, <strong>class.phpmailer.php<\/strong> and <strong>class.smtp.php<\/strong>. You can download and copy these two files from link given below.<\/p>\n<p><a title=\"Download PHPMailer\" href=\"https:\/\/codeload.github.com\/PHPMailer\/PHPMailer\/zip\/master\" target=\"_blank\" rel=\"noopener\"><strong>External link: Download PHPMailer library.<\/strong><\/a><\/p>\n<p>Once download place those two files plus the one contains above function. Here is a link of the file.<\/p>\n<p><a title=\"Download and view\" href=\"https:\/\/github.com\/shaikh-shahid\/phpmailer-function\/blob\/master\/sendmail.php\" target=\"_blank\" rel=\"noopener\">Internal link : Download custom phpmailer function.<\/a><br \/>\n<code lang=\"php\"><br \/>\n<?php \n      include(\"sendmail.php\");\n      $to       =   \"some email\";\n      $subject  =   \"Hello\";\n      $message  =   \"hello <i>how are you.<\/i>\";<br \/>\n      $name     =   \"Shahid Shaikh\";<br \/>\n      $mailsend =   sendmail($to,$subject,$message,$name);<br \/>\n      if($mailsend==1){<br \/>\n        echo '<\/p>\n<h2>email sent.<\/h2>\n<p>';<br \/>\n      }<br \/>\n      else{<br \/>\n        echo '<\/p>\n<h2>There are some issue.<\/h2>\n<p>';<br \/>\n      }<br \/>\n?><br \/>\n<\/code><\/p>\n<h2>Performance study:<\/h2>\n<p>Recently i was assigned to one project in my company and in that we were using <a href=\"https:\/\/codeforgeek.com\/ajax-search-box-php-mysql\/\" title=\"Ajax Search Box in PHP and MySQL\" target=\"_blank\" rel=\"noopener\">php <\/a><strong>mail()<\/strong> function. This function used to take like 30 seconds to send one email. Reason was the heavy production server but obviously this is not what client expects.<\/p>\n<p>So i changed it to PHPMailer and after using the Google app smtp i am able to send email within 3 seconds at top. Reduction of 27 seconds and guaranteed delivery of email.<\/p>\n<h2>Further study:<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\" title=\"PHPMailer official site\" target=\"_blank\" rel=\"noopener\">GitHub &#8211; phpmailer.<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\" title=\"PHPMailer Git repository\" target=\"_blank\" rel=\"noopener\">Official GIT repository of PHPMailer.<\/a>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PHP have inbuilt mail()\u00a0function to send email right from the code. This is easy to use and effective too but it has one serious problem. It gives load to production server and also provide no guarantee about email delivery. PHPMailer\u00a0is one of the popular and easy to use email library for php. This solves the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":773,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[15,18],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner.png",900,400,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner-300x133.png",300,133,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner-768x341.png",768,341,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner.png",900,400,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner.png",900,400,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner.png",900,400,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"PHP have inbuilt mail()\u00a0function to send email right from the code. This is easy to use and effective too but it has one serious problem. It gives load to production server and also provide no guarantee about email delivery. PHPMailer\u00a0is one of the popular and easy to use email library for php. This solves the&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/773"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}