{"id":94,"date":"2015-04-27T10:55:30","date_gmt":"2015-04-27T10:55:30","guid":{"rendered":"http:\/\/betakb.veeble.org\/?p=94"},"modified":"2025-04-24T11:38:33","modified_gmt":"2025-04-24T06:08:33","slug":"sending-mails-via-php-script","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/sending-mails-via-php-script\/","title":{"rendered":"Sending mails via PHP script"},"content":{"rendered":"<h2><span id=\"How_to_Send_Email_from_a_PHP_Script\" class=\"mw-headline\">How to Send Email from a PHP Script<\/span><\/h2>\n<p>PHP seems to make most things extremely easy. Sending mail is no exception.<\/p>\n<p><b>Send Email from a PHP Script<\/b><\/p>\n<p>All it takes is the right configuration (to send mail using a local or a remote server) and one function: mail().<\/p>\n<p><b>Send Email from a PHP Script Example<\/b><\/p>\n<p>The first argument to this function is the recipient, the second specifies the message&#8217;s subject and the third one should contain the body. So to send a simple sample message, we could use:<\/p>\n<pre>&lt;?php\n$to = \"recipient@example.com\";\n$subject = \"Hi!\";\n$body = \"Hi,\\n\\nHow are you?\";\nif (mail($to, $subject, $body))\n{\necho(\"&lt;p&gt;Message successfully sent!&lt;\/p&gt;\");\n}\nelse \n{\necho(\"&lt;p&gt;Message delivery failed...&lt;\/p&gt;\");\n}\n?&gt;\n<\/pre>\n<p>That&#8217;s it! Note that you can have PHP validate your email addresses for correctness before sending.<\/p>\n<p>Use Custom Headers (e.g. &#8220;From:&#8221;) in Mail from a PHP Script<\/p>\n<p>Do you want to set a custom From: address, maybe taken from the form you send, or another custom header line? It is but an additional argument you need.<\/p>\n<p><b>Protecting Your Script from Spammer Exploit<\/b><\/p>\n<p>If you use the mail() function (in combination with a web form in particular), make sure you check it is called from the desired page and protect the form with a CAPTCHA maybe. You can also check for suspicious strings in any arguments (say, &#8220;Bcc:&#8221; followed by a number of email addresses).<\/p>\n<p><b>Send Email from a PHP Script with SMTP Authentication<\/b><\/p>\n<p>If mail() does not work for you, you have options, too. The mail() function included with stock PHP does not support SMTP authentication, for example. If mail() does not work for you for this or another reason, try the PEAR Mail package, which is much more comprehensive and sending mail almost as easily from your PHP scripts.<\/p>\n<h2><span id=\"How_to_Send_Email_from_a_PHP_Script_Using_SMTP_Authentication\" class=\"mw-headline\">How to Send Email from a PHP Script Using SMTP Authentication<\/span><\/h2>\n<p><b>PHP mail() and SMTP Authentication<\/b><\/p>\n<p>Part of what makes the PHP mail() function is so simple is its lack of flexibility. Most importantly and frustratingly, the stock mail() does not usually allow you to use the SMTP server of your choice, and it does not support SMTP authentication, required by many a mail server today, at all.<\/p>\n<p>Fortunately, overcoming PHP&#8217;s built-in shortcomings need not be difficult, complicated or painful either. For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too. For enhanced security, secure SSL connections are supported.<\/p>\n<p><b>Send Email from a PHP Script Using SMTP Authentication<\/b><\/p>\n<p>To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:<\/p>\n<p>Make sure the <b>PEAR<\/b> Mail package is installed.<\/p>\n<p>Typically, in particular with PHP 4 or later, this will have already been done for you. Just give it a try.<\/p>\n<p>Adapt the example below for your needs. Make sure you change the following variables at least:<\/p>\n<p><i>from: the email address from which you want the message to be sent.<\/i><\/p>\n<p><i>to: the recipient&#8217;s email address and name.<\/i><\/p>\n<p><i>host: your outgoing SMTP server name.<\/i><\/p>\n<p><i>username: the SMTP user name (typically the same as the user name used to retrieve mail).<\/i><\/p>\n<p><i>password: the password for SMTP authentication.<\/i><\/p>\n<p><b>Sending Mail from PHP Using SMTP Authentication &#8211; Example<\/b><\/p>\n<pre>&lt;?php\nrequire_once \"Mail.php\";\n\n$from = \"Sandra Sender &lt;sender@example.com&gt;\";\n$to = \"Ramona Recipient &lt;recipient@example.com&gt;\";\n$subject = \"Hi!\";\n$body = \"Hi,\\n\\nHow are you?\";\n\n$host = \"mail.example.com\";\n$username = \"smtp_username\";\n$password = \"smtp_password\";\n\n$headers = array ('From' =&gt; $from,\n  'To' =&gt; $to,\n  'Subject' =&gt; $subject);\n$smtp = Mail::factory('smtp',\n  array ('host' =&gt; $host,\n    'auth' =&gt; true,\n    'username' =&gt; $username,\n    'password' =&gt; $password));\n\n$mail = $smtp-&gt;send($to, $headers, $body);\n\nif (PEAR::isError($mail)) {\n  echo(\"&lt;p&gt;\" . $mail-&gt;getMessage() . \"&lt;\/p&gt;\");\n } else {\n  echo(\"&lt;p&gt;Message successfully sent!&lt;\/p&gt;\");\n }\n?&gt;\n<\/pre>\n<p><b>Sending Mail from PHP Using SMTP Authentication and SSL Encryption &#8211; Example<\/b><\/p>\n<pre>&lt;?php\nrequire_once \"Mail.php\";\n\n$from = \"Sandra Sender &lt;sender@example.com&gt;\";\n$to = \"Ramona Recipient &lt;recipient@example.com&gt;\";\n$subject = \"Hi!\";\n$body = \"Hi,\\n\\nHow are you?\";\n\n$host = \"ssl:\/\/mail.example.com\";\n$port = \"465\";\n$username = \"smtp_username\";\n$password = \"smtp_password\";\n\n$headers = array ('From' =&gt; $from,\n  'To' =&gt; $to,\n  'Subject' =&gt; $subject);\n$smtp = Mail::factory('smtp',\n  array ('host' =&gt; $host,\n    'port' =&gt; $port,\n    'auth' =&gt; true,\n    'username' =&gt; $username,\n    'password' =&gt; $password));\n\n$mail = $smtp-&gt;send($to, $headers, $body);\n\nif (PEAR::isError($mail)) {\n  echo(\"&lt;p&gt;\" . $mail-&gt;getMessage() . \"&lt;\/p&gt;\");\n } else {\n  echo(\"&lt;p&gt;Message successfully sent!&lt;\/p&gt;\");\n }\n?&gt;<\/pre>\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-d10f6a72 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Expert Python Support<\/h3><p class=\"uagb-cta__desc\">Get dedicated support for your Python hosting needs from our expert team. We&#8217;re here to help.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/python-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Deploy Now<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to Send Email from a PHP Script PHP seems to make most things extremely easy. Sending mail is no exception. Send Email from [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":8714,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","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":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","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-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[6],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mail"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Sending-mails-via-PHP-script.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Digin Dominic","author_link":"https:\/\/www.veeble.com\/kb\/author\/digin-dominic\/"},"uagb_comment_info":0,"uagb_excerpt":"How to Send Email from a PHP Script PHP seems to make most things extremely easy. Sending mail is no exception. Send Email from [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":2,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":8357,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/94\/revisions\/8357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8714"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}