{"id":16715,"date":"2018-02-10T23:40:29","date_gmt":"2018-02-10T23:40:29","guid":{"rendered":"https:\/\/www.muvipa.com.mx\/docs\/examples-using-phpmailer\/"},"modified":"2018-02-10T23:40:29","modified_gmt":"2018-02-10T23:40:29","slug":"examples-using-phpmailer","status":"publish","type":"page","link":"https:\/\/www.muvipa.com.mx\/?page_id=16715","title":{"rendered":"Examples using phpmailer"},"content":{"rendered":"<h2>Examples using PHPMailer<\/h2>\n<h3>1. Advanced Example<\/h3>\n<p>This demonstrates sending multiple email messages with binary attachments<br \/>\nfrom a MySQL database using multipart\/alternative messages.<\/p>\n<\/p>\n<pre>\nrequire 'PHPMailerAutoload.php';\n\n$mail = new PHPMailer();\n\n$mail-&gt;setFrom('list@example.com', 'List manager');\n$mail-&gt;Host   = 'smtp1.example.com;smtp2.example.com';\n$mail-&gt;Mailer = 'smtp';\n\n@mysqli_connect('localhost','root','password');\n@mysqli_select_db(\"my_company\");\n$query = \"SELECT full_name, email, photo FROM employee\";\n$result = @mysqli_query($query);\n\nwhile ($row = mysqli_fetch_assoc($result))\n{\n    \/\/ HTML body\n    $body  = \"Hello &lt;font size=\"4\"&gt;\" . $row['full_name'] . \"&lt;\/font&gt;, &lt;p&gt;\";\n    $body .= \"&lt;i&gt;Your&lt;\/i&gt; personal photograph to this message.&lt;p&gt;\";\n    $body .= \"Sincerely, &lt;br&gt;\";\n    $body .= \"phpmailer List manager\";\n\n    \/\/ Plain text body (for mail clients that cannot read HTML)\n    $text_body  = 'Hello ' . $row['full_name'] . \", nn\";\n    $text_body .= \"Your personal photograph to this message.nn\";\n    $text_body .= \"Sincerely, n\";\n    $text_body .= 'phpmailer List manager';\n\n    $mail-&gt;Body    = $body;\n    $mail-&gt;AltBody = $text_body;\n    $mail-&gt;addAddress($row['email'], $row['full_name']);\n    $mail-&gt;addStringAttachment($row['photo'], 'YourPhoto.jpg');\n\n    if(!$mail-&gt;send())\n        echo \"There has been a mail error sending to \" . $row['email'] . \"&lt;br&gt;\";\n\n    \/\/ Clear all addresses and attachments for next loop\n    $mail-&gt;clearAddresses();\n    $mail-&gt;clearAttachments();\n}\n<\/pre>\n<\/p>\n<h3>2. Extending PHPMailer<\/h3>\n<p>Extending classes with inheritance is one of the most<br \/>\npowerful features of object-oriented programming. It allows you to make changes to the<br \/>\noriginal class for your own personal use without hacking the original<br \/>\nclasses, and it&#8217;s very easy to do:<\/p>\n<p>\nHere&#8217;s a class that extends the phpmailer class and sets the defaults<br \/>\nfor the particular site:<br \/>\nPHP include file: my_phpmailer.php\n<\/p>\n<\/p>\n<pre>\nrequire 'PHPMailerAutoload.php';\n\nclass my_phpmailer extends PHPMailer {\n    \/\/ Set default variables for all new objects\n    public $From     = 'from@example.com';\n    public $FromName = 'Mailer';\n    public $Host     = 'smtp1.example.com;smtp2.example.com';\n    public $Mailer   = 'smtp';                         \/\/ Alternative to isSMTP()\n    public $WordWrap = 75;\n\n    \/\/ Replace the default debug output function\n    protected function edebug($msg) {\n        print('My Site Error');\n        print('Description:');\n        printf('%s', $msg);\n        exit;\n    }\n\n    \/\/Extend the send function\n    public function send() {\n        $this-&gt;Subject = '[Yay for me!] '.$this-&gt;Subject;\n        return parent::send()\n    }\n\n    \/\/ Create an additional function\n    public function do_something($something) {\n        \/\/ Place your new code here\n    }\n}\n<\/pre>\n<p>\nNow here&#8217;s a normal PHP page in the site, which will have all the defaults set above:<\/p>\n<pre>\nrequire 'my_phpmailer.php';\n\n\/\/ Instantiate your new class\n$mail = new my_phpmailer;\n\n\/\/ Now you only need to add the necessary stuff\n$mail-&gt;addAddress('josh@example.com', 'Josh Adams');\n$mail-&gt;Subject = 'Here is the subject';\n$mail-&gt;Body    = 'This is the message body';\n$mail-&gt;addAttachment('c:\/temp\/11-10-00.zip', 'new_name.zip');  \/\/ optional name\n\nif(!$mail-&gt;send())\n{\n   echo 'There was an error sending the message';\n   exit;\n}\n\necho 'Message was sent successfully';\n<\/pre>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Examples using PHPMailer 1. Advanced Example This demonstrates sending multiple email messages with binary attachments from a MySQL database using multipart\/alternative messages. require &#8216;PHPMailerAutoload.php&#8217;; $mail = new PHPMailer(); $mail-&gt;setFrom(&#8216;list@example.com&#8217;, &#8216;List manager&#8217;); $mail-&gt;Host = &#8216;smtp1.example.com;smtp2.example.com&#8217;; $mail-&gt;Mailer = &#8216;smtp&#8217;; @mysqli_connect(&#8216;localhost&#8217;,&#8217;root&#8217;,&#8217;password&#8217;); @mysqli_select_db(\u00abmy_company\u00bb); $query = \u00abSELECT full_name, email, photo FROM employee\u00bb; $result = @mysqli_query($query); while ($row = mysqli_fetch_assoc($result)) { [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":16714,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-16715","page","type-page","status-publish","hentry","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Examples using phpmailer - Museo Virtual de Pachuca<\/title>\n<meta name=\"description\" content=\"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.\" \/>\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.muvipa.com.mx\/?page_id=16715\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Examples using phpmailer - Museo Virtual de Pachuca\" \/>\n<meta property=\"og:description\" content=\"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.muvipa.com.mx\/?page_id=16715\" \/>\n<meta property=\"og:site_name\" content=\"Museo Virtual de Pachuca\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/museovirtualpachuca\/\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@MUSEOMUVIPA\" \/>\n<meta name=\"twitter:label1\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16715\",\"url\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16715\",\"name\":\"Examples using phpmailer - Museo Virtual de Pachuca\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#website\"},\"datePublished\":\"2018-02-10T23:40:29+00:00\",\"description\":\"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16715#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16715\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16715#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\\\/\\\/www.muvipa.com.mx\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Docs\",\"item\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?page_id=16714\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Examples using phpmailer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#website\",\"url\":\"https:\\\/\\\/www.muvipa.com.mx\\\/\",\"name\":\"MUVIPA\",\"description\":\"Museo Virtual de Pachuca\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.muvipa.com.mx\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#organization\",\"name\":\"Museo Virtual de Pachuca\",\"url\":\"https:\\\/\\\/www.muvipa.com.mx\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.muvipa.com.mx\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/cropped-principal.jpg\",\"contentUrl\":\"https:\\\/\\\/www.muvipa.com.mx\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/cropped-principal.jpg\",\"width\":512,\"height\":512,\"caption\":\"Museo Virtual de Pachuca\"},\"image\":{\"@id\":\"https:\\\/\\\/www.muvipa.com.mx\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/museovirtualpachuca\\\/\",\"https:\\\/\\\/x.com\\\/MUSEOMUVIPA\",\"https:\\\/\\\/www.instagram.com\\\/muvipa\\\/\",\"https:\\\/\\\/www.pinterest.com.mx\\\/holamuvipa\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCG-A1s9_Q6fGShym1BHj6Hg\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Examples using phpmailer - Museo Virtual de Pachuca","description":"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.","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.muvipa.com.mx\/?page_id=16715","og_locale":"es_ES","og_type":"article","og_title":"Examples using phpmailer - Museo Virtual de Pachuca","og_description":"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.","og_url":"https:\/\/www.muvipa.com.mx\/?page_id=16715","og_site_name":"Museo Virtual de Pachuca","article_publisher":"https:\/\/www.facebook.com\/museovirtualpachuca\/","twitter_card":"summary_large_image","twitter_site":"@MUSEOMUVIPA","twitter_misc":{"Tiempo de lectura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.muvipa.com.mx\/?page_id=16715","url":"https:\/\/www.muvipa.com.mx\/?page_id=16715","name":"Examples using phpmailer - Museo Virtual de Pachuca","isPartOf":{"@id":"https:\/\/www.muvipa.com.mx\/#website"},"datePublished":"2018-02-10T23:40:29+00:00","description":"Conoce m\u00e1s sobre la Ciudad de Pachuca a trav\u00e9s del museo virtual de Pachuca, descubre las galer\u00edas, juegos y articulos que tenemos para ti.","breadcrumb":{"@id":"https:\/\/www.muvipa.com.mx\/?page_id=16715#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.muvipa.com.mx\/?page_id=16715"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.muvipa.com.mx\/?page_id=16715#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/www.muvipa.com.mx\/"},{"@type":"ListItem","position":2,"name":"Docs","item":"https:\/\/www.muvipa.com.mx\/?page_id=16714"},{"@type":"ListItem","position":3,"name":"Examples using phpmailer"}]},{"@type":"WebSite","@id":"https:\/\/www.muvipa.com.mx\/#website","url":"https:\/\/www.muvipa.com.mx\/","name":"MUVIPA","description":"Museo Virtual de Pachuca","publisher":{"@id":"https:\/\/www.muvipa.com.mx\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.muvipa.com.mx\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Organization","@id":"https:\/\/www.muvipa.com.mx\/#organization","name":"Museo Virtual de Pachuca","url":"https:\/\/www.muvipa.com.mx\/","logo":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/www.muvipa.com.mx\/#\/schema\/logo\/image\/","url":"https:\/\/www.muvipa.com.mx\/wp-content\/uploads\/2018\/09\/cropped-principal.jpg","contentUrl":"https:\/\/www.muvipa.com.mx\/wp-content\/uploads\/2018\/09\/cropped-principal.jpg","width":512,"height":512,"caption":"Museo Virtual de Pachuca"},"image":{"@id":"https:\/\/www.muvipa.com.mx\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/museovirtualpachuca\/","https:\/\/x.com\/MUSEOMUVIPA","https:\/\/www.instagram.com\/muvipa\/","https:\/\/www.pinterest.com.mx\/holamuvipa\/","https:\/\/www.youtube.com\/channel\/UCG-A1s9_Q6fGShym1BHj6Hg"]}]}},"brizy_media":[],"builder_content":"","_links":{"self":[{"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/pages\/16715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16715"}],"version-history":[{"count":0,"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/pages\/16715\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=\/wp\/v2\/pages\/16714"}],"wp:attachment":[{"href":"https:\/\/www.muvipa.com.mx\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}