{"id":12411,"date":"2016-05-16T16:15:47","date_gmt":"2016-05-16T13:15:47","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=12411"},"modified":"2018-01-09T10:59:13","modified_gmt":"2018-01-09T08:59:13","slug":"php-mail-function-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/","title":{"rendered":"PHP Mail Function Example"},"content":{"rendered":"<p>In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is especially useful in scenarios like the following:<\/p>\n<ul>\n<li>Registration confirmation.<\/li>\n<li>Password recovery.<\/li>\n<li>Notification delivery.<\/li>\n<\/ul>\n<p>And any other scenario where a service needs to contact a user.<\/p>\n<p>PHP has a built-in function called <code>mail<\/code> that will do all the job but, first, we have to make some configurations in order to make this function work.<\/p>\n<p>For this tutorial, we will use:<\/p>\n<ul>\n<li>Ubuntu (14.04) as Operating System.<\/li>\n<li>Apache HTTP server (2.4.7).<\/li>\n<li>PHP (5.5.9).<\/li>\n<li>sSMTP (2.64), a lightweight MTA (Mail Transfer Agent).<\/li>\n<\/ul>\n<p>[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip environment preparation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<h2>1. Preparing the environment<\/h2>\n<h3>1.1. Installation<\/h3>\n<p>Below, the commands to install Apache and PHP are shown:<\/p>\n<pre class=\"brush:bash\">sudo apt-get update\r\nsudo apt-get install apache2 php5 libapache2-mod-php5\r\nsudo service apache2 restart<\/pre>\n<p>Now, we install sSMTP:<\/p>\n<pre class=\"brush:bash\">sudo apt-get install ssmtp<\/pre>\n<p>With this MTA, now, we will be able to send mails from our machine, to a SMTP server. In other words, we can make a SMTP server (like Gmail&#8217;s, Outlook&#8217;s, etc.) deliver the mails we want to send.<\/p>\n<h3>1.2. sSMTP configuration<\/h3>\n<p>The sSMTP configuration consists of two things:<\/p>\n<ul>\n<li>Defining the SMTP service we are going to use.<\/li>\n<li>Provide credentials for that service.<\/li>\n<\/ul>\n<p>In this example, we will use Google&#8217;s Gmail SMTP server., and supposing that we have an account named <em>mygmailaccount@gmail.com,<\/em> with <em>mygmailpassword<\/em> as password.<\/p>\n<p>So, taking that into account, the <code>\/etc\/ssmtp\/ssmtp.conf<\/code> file will remain as:<\/p>\n<p><em><span style=\"text-decoration: underline;\">ssmtp.conf<\/span><\/em><\/p>\n<pre class=\"brush:bash\">UseSTARTTLS=YES\r\nmailhub=smtp.gmail.com:587\r\nAuthUser=mygmailaccount@gmail.com\r\nAuthPass=mygmailpassword<\/pre>\n<p>This is what we are setting:<\/p>\n<ul>\n<li><em>UseSTARTTLS<\/em>: necessary for using Gmail&#8217;s SSL\/TLS sever.<\/li>\n<li><em>mailhub<\/em>: the smtp server itself, in port 587, for SSL\/TLS.<\/li>\n<li><em>AuthUser<\/em> and <em>AuthPass<\/em>: credentials for loging in Gmail.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> take care of who has access to this file &#8211; anyone having access to the file, can access the mail account set.<\/p>\n<h3>1.3. PHP configuration<\/h3>\n<p>The last step, is to make PHP now how to use sSMTP. For that, we have to specify the path to sSMTP executable, in PHP config file, <code>\/etc\/php5\/apache2\/php.ini<\/code> file:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>php.ini<\/em><\/span><\/p>\n<pre class=\"brush:bash\">sendmail_path = \/usr\/sbin\/sendmail\r\n<\/pre>\n<p>Don&#8217;t forget to restart Apache after making any change in PHP configuration:<\/p>\n<pre class=\"brush:bash\">sudo service apache2 restart<\/pre>\n<h2><span id=\"code\">2. PHP mail sending script<\/span><\/h2>\n<p>The PHP code is all about calling a function, named mail. Here an example that sends a mail to someone, with a subject and the message itself:<\/p>\n<p><em><span style=\"text-decoration: underline;\">send_mail_example.php<\/span><\/em><\/p>\n<pre class=\"brush:php; highlight:[7]\">&lt;?php\r\n\r\n$to = 'someone1@mail.com, someone2@mail.com';\r\n$subject = 'Mail from PHP';\r\n$message = \"Hi,\\r\\nThis mail has been sent using PHP!\";\r\n\r\n$success = mail($to, $subject, $message);\r\n\r\nif ($success) {\r\n    $response = 'Message has been sent successfully.';\r\n} else {\r\n    $response = 'The message could not be sent.';\r\n}\r\n\r\necho $response;\r\n<\/pre>\n<p>There are a few things that have to be taken into account:<\/p>\n<ul>\n<li>It is possible to set multiple mails to send the mail to, separated by commas, as in the example above.<\/li>\n<li><code>mail<\/code> function does not perform any encoding. This can be a problem, that we will see below how to deal with.<\/li>\n<li>The lines must be separated with Windows line ending format, i.e., the CRLF: <code>'\\r\\n'<\/code>.<\/li>\n<li>Unfortunately, <code>mail<\/code> function only returns a boolean value so, if something goes wrong, we cannot have any details at runtime.<\/li>\n<\/ul>\n<p>Mail function has two more optional parameters: for setting additional headers, and for setting additional parameters. The first one is to add additional headers, as we would do, for example, in a HTML page. The last one is, actually, a MTA command line parameter, used to pass additional flags and commands.<\/p>\n<p>Let&#8217;s see how can we use these additional options.<\/p>\n<h3>2.1. Setting additional headers<\/h3>\n<p>Consider the following scenario: we have a system that sends greetings to the users registered within, each one in the language set by the user. If we have, for example, a Chinese user, the message body would be:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>send_mail_example.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">$message = '\u4f60\u597d'; \/\/ 'Hello' in Chinese.<\/pre>\n<p>With the example we saw above, the result would be an ugly string of characters that don&#8217;t match with the expected Chinese characters. For a scenario like this, is not enough to rely the encoding only in the language setting. The solution would be to encode the message properly, encoding it to UTF-8, and, for that, we have to set the header manually.<\/p>\n<p>For that, we need to perform two additional steps:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>send_mail_example.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">$headers = 'Content-Type: text\/plain; charset=UTF-8';\r\n\r\n$success = mail($to, $subject, $message, $headers);\r\n<\/pre>\n<p>Quite simple: define the headers, and pass them to the function. If we try now the script, we will receive a mail with those nice Chinese characters.<\/p>\n<p>It is also usual to use this additional headers, for example, to add carbon copies (cc) and blind carbon copies (bcc).<\/p>\n<p><strong>Note:<\/strong> Multiple headers must be separated as same as new lines in the message, with a CRLF.<\/p>\n<h3>2.2. Setting additional parameters<\/h3>\n<p>As told before, when we are setting additional parameters, we are actually passing instructions that will be executed by the server, te binary we set to <code>sendmail_path<\/code>, in <code>php.ini<\/code>.<\/p>\n<p>For example, we could set the verbose mode, to see how is going the whole process, which can be useful for troubleshooting.<\/p>\n<p>For this, we just declare another variable, for the parameters, and we pass it as last parameter to the function:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>send_mail_example.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">$parameters = '-v';\r\n\r\n$success = mail($to, $subject, $message, $headers, $parameters);\r\n<\/pre>\n<p>If we execute now the script, we will see the whole process: from the initial handshake with the server, to the connection close.<\/p>\n<p>In this case, to pass several parameters, they must be separated by a blank space.<\/p>\n<p>You can see the whole list of available options in sSMTP manual:<\/p>\n<pre class=\"brush:bash\">man ssmtp<\/pre>\n<h2>3. Troubleshooting<\/h2>\n<p>If you have not been able to make this example work, in this section we are going to see which can be the possible causes, and finding solutions for them.<\/p>\n<h3>3.1. Set verbose mode<\/h3>\n<p>Before doing anything else, the first recommended step would be to set the verbose mode, to see in detail what&#8217;s happening. You can see how to do it in section 2.2.<\/p>\n<h3>3.2. Login is rejected by SMTP server<\/h3>\n<p>It may occur that Gmail considered the login attempt through sSMTP as insecure or suspicious, because of trying to login using an external service or app.<\/p>\n<p>To confirm that this is the problem, login into the account you set in sSMTP configuration file, and you should have received and email from Google with a subject similar to:<\/p>\n<blockquote><p>Suspicious sign in prevented.<\/p><\/blockquote>\n<p>To disable this security measure, in Gmail, go to My Account\/Sign-in &amp; security, and in Connected apps &amp; sites section, enable the &#8220;Allow less secure apps&#8221; toggle to &#8220;on&#8221;.<\/p>\n<h3>3.3. Firewall is filtering outgoing traffic<\/h3>\n<p>Maybe, a firewall is filtering the connection. Normally, by default, firewalls do not filter outgoing traffic, but it can be a possibility. Check that you don&#8217;t have any firewall filtering outgoing traffic that would affect to this example (remember that we configured the MTA to use the port 587).<\/p>\n<h3>3.4. Check PHP error log<\/h3>\n<p>If it continues not working, check the PHP error log, to see which is the error thrown by PHP, to get more hints of which can be the error. By default, the error log is located in <code>\/var\/log\/apache2\/error.log<\/code>.<\/p>\n<h2>4. Alternatives to sSMTP<\/h2>\n<p>For this example, we have chosen sSMTP as it is the easiest and fastest way to deliver mails, only needing the simple configuration we saw in section 1.2, and also because it is very light; is not using daemons, and its impact in CPU and memory is negligible.<\/p>\n<p>But, if you are looking for a more advanced, full-featured MTA, <strong>Postfix<\/strong> and <strong>Exim<\/strong> are probably the most used ones.<\/p>\n<h2>5. Summary<\/h2>\n<p>In this example, we have seen how to send mails with PHP, which is pretty simple &#8211; only a function call is needed. But we have also seen that, that alone is not enough, and that a MTA is needed, to connect with the SMTP server we are using. We have also analysed the most common errors that we can face in this task, and how to solve them, apart from the problem that can suppose the sending of not properly encoded mails.<\/p>\n<h2>6. Download the source code<\/h2>\n<p>This was an example of mail sending with PHP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/PHPMailFunctionExample.zip\"><strong>PHPMailFunctionExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is especially useful in scenarios like the following: Registration confirmation. Password recovery. Notification delivery. And any other scenario where a service needs to contact a user. PHP has a &hellip;<\/p>\n","protected":false},"author":160,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[142,122,338],"class_list":["post-12411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-email","tag-php","tag-smtp"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Mail Function Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is\" \/>\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.webcodegeeks.com\/php\/php-mail-function-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Mail Function Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-16T13:15:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:59:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Toni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Toni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\"},\"author\":{\"name\":\"Toni\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\"},\"headline\":\"PHP Mail Function Example\",\"datePublished\":\"2016-05-16T13:15:47+00:00\",\"dateModified\":\"2018-01-09T08:59:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\"},\"wordCount\":1278,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"keywords\":[\"email\",\"php\",\"smtp\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\",\"name\":\"PHP Mail Function Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-05-16T13:15:47+00:00\",\"dateModified\":\"2018-01-09T08:59:13+00:00\",\"description\":\"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Mail Function Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\",\"name\":\"Toni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"Toni\"},\"url\":\"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Mail Function Example - Web Code Geeks - 2026","description":"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is","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.webcodegeeks.com\/php\/php-mail-function-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP Mail Function Example - Web Code Geeks - 2026","og_description":"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-05-16T13:15:47+00:00","article_modified_time":"2018-01-09T08:59:13+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Toni","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Toni","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/"},"author":{"name":"Toni","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966"},"headline":"PHP Mail Function Example","datePublished":"2016-05-16T13:15:47+00:00","dateModified":"2018-01-09T08:59:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/"},"wordCount":1278,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","keywords":["email","php","smtp"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/","name":"PHP Mail Function Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-05-16T13:15:47+00:00","dateModified":"2018-01-09T08:59:13+00:00","description":"In this example, we will see how we can send emails with PHP. After seeing this example, you will be able to send emails programmatically, which is","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-mail-function-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Mail Function Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966","name":"Toni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"Toni"},"url":"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/160"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=12411"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=12411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=12411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=12411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}