{"id":2431,"date":"2021-08-04T11:32:47","date_gmt":"2021-08-04T11:32:47","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2431"},"modified":"2025-04-06T08:02:54","modified_gmt":"2025-04-06T08:02:54","slug":"php-mail","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-mail\/","title":{"rendered":"PHP mail"},"content":{"rendered":"\r\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to send email using the PHP <code>mail()<\/code> function.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-mail-function'>Introduction to the PHP mail() function <a href=\"#introduction-to-the-php-mail-function\" class=\"anchor\" id=\"introduction-to-the-php-mail-function\" title=\"Anchor for Introduction to the PHP mail() function\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To send mail, you use the <code>mail()<\/code> function.<\/p>\r\n\r\n\r\n\r\n<p>On Linux or Unix systems, you can configure the <code>mail()<\/code> function to use the <code>sednmail<\/code> or <code>Qmail<\/code> program to send messages.<\/p>\r\n\r\n\r\n\r\n<p>On Windows, you can install the <code>sendmail<\/code> and set the <code>sendmail_path<\/code> in <code>php.ini<\/code> file to point at the executable file.<\/p>\r\n\r\n\r\n\r\n<p>However, it&#8217;s more convenient to set the SMTP server with a port and <code>sendmail_from<\/code> in the <code>php.ini<\/code> file on Windows like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&#91;mail <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>]\r\n<span class=\"hljs-title\">SMTP<\/span>=<span class=\"hljs-title\">smtp<\/span>.<span class=\"hljs-title\">phptutorial<\/span>.<span class=\"hljs-title\">net<\/span>\r\n<span class=\"hljs-title\">smtp_port<\/span>=25\r\n<span class=\"hljs-title\">sendmail_from<\/span>=<span class=\"hljs-title\">contact<\/span>@<span class=\"hljs-title\">phptutorial<\/span>.<span class=\"hljs-title\">net<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>If the SMTP server requires authentication, you can add the following lines for the account to authenticate:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">auth_username=smtp_user\r\nauth_password=smpt_password<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Once the configuration is ready, you need to restart the webserver.<\/p>\r\n\r\n\r\n\r\n<p>The following illustrates the syntax of the <code>mail()<\/code> function:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">mail(\r\n    string $to,\r\n    string $subject,\r\n    string $message,\r\n    <span class=\"hljs-keyword\">array<\/span>|string $additional_headers = &#91;],\r\n    string $additional_params = <span class=\"hljs-string\">\"\"<\/span>\r\n): bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The <code>mail()<\/code> function has the following parameters:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>$to<\/code> is the receiver of the email<\/li>\r\n<li><code>$subject<\/code> is the email subject.<\/li>\r\n<li><code>$message<\/code> is the email message. It can be plain text or HTML. If <code>$message<\/code> is plain text, you use a CRLF (\\r\\n) to separate lines. Each line should not exceed 70 characters.<\/li>\r\n<li><code>$additional_headers<\/code> is a string or an array inserted at the email&#8217;s header. It includes from, cc, bcc&#8230; If the header comes from an untrusted source, you should always sanitize it for security.<\/li>\r\n<li><code>$additional_params<\/code> allows you to pass additional flags as the command-line options to the <code>sendmail<\/code> program.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>The <code>mail()<\/code> function returns <code>true<\/code> if the mail was accepted for delivery. It doesn&#8217;t mean that the mail is successfully reached the intended receiver. If an error occurred, the <code>mail()<\/code> function return <code>false<\/code>.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='php-mail-function-examples'>PHP mail() function examples <a href=\"#php-mail-function-examples\" class=\"anchor\" id=\"php-mail-function-examples\" title=\"Anchor for PHP mail() function examples\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>Let&#8217;s take some examples of using the PHP <code>mail()<\/code> function.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='1-using-the-php-mail-function-to-send-a-plain-text-email-example'>1) Using the PHP mail() function to send a plain text email example <a href=\"#1-using-the-php-mail-function-to-send-a-plain-text-email-example\" class=\"anchor\" id=\"1-using-the-php-mail-function-to-send-a-plain-text-email-example\" title=\"Anchor for 1) Using the PHP mail() function to send a plain text email example\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>The following example uses the <code>mail()<\/code> function to send a simple email:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$subject = <span class=\"hljs-string\">'This is a test email'<\/span>;\r\n\r\n$message = <span class=\"hljs-string\">&lt;&lt;&lt;MSG\r\n    Hi,\r\n    This is a simple email.\r\n    It's sent from PHP.\r\nMSG;<\/span>\r\n\r\nwordwrap($message, <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">\"\\r\\n\"<\/span>);\r\n\r\nmail(<span class=\"hljs-string\">'contact@phptutorial.net'<\/span>, $subject, $message);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this example, we use the <code>wordwrap()<\/code> function to ensure that the lines of the message won&#8217;t exceed 70 characters.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='2-using-the-php-mail-function-to-send-a-mail-with-extra-headers-example'>2) Using the PHP mail() function to send a mail with extra headers example <a href=\"#2-using-the-php-mail-function-to-send-a-mail-with-extra-headers-example\" class=\"anchor\" id=\"2-using-the-php-mail-function-to-send-a-mail-with-extra-headers-example\" title=\"Anchor for 2) Using the PHP mail() function to send a mail with extra headers example\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>The following example uses the <code>mail()<\/code> function to send a mail with additional headers like From, Reply-To, and X-Mailer:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$to      = <span class=\"hljs-string\">'contact@phptutorial.net'<\/span>;\r\n$subject = <span class=\"hljs-string\">'This is a test email'<\/span>;\r\n$message = <span class=\"hljs-string\">'Hi there'<\/span>;\r\n\r\n$headers&#91;] = <span class=\"hljs-string\">'From: john.doe@example.com'<\/span>;\r\n$headers&#91;] = <span class=\"hljs-string\">'Reply-To: john.doe@example.com'<\/span>;\r\n$headers&#91;] = <span class=\"hljs-string\">'X-Mailer: PHP\/'<\/span> . phpversion();\r\n\r\n\r\nmail($to, $subject, $message, implode(<span class=\"hljs-string\">'\\r\\n'<\/span>, $headers));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>If you use PHP 7.2 or later, you can pass the headers as an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-in_array\/\">array<\/a> like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$to      = <span class=\"hljs-string\">'contact@phptutorial.net'<\/span>;\r\n$subject = <span class=\"hljs-string\">'This is a test email'<\/span>;\r\n$message = <span class=\"hljs-string\">'Hi there'<\/span>;\r\n\r\n$headers = &#91;\r\n    <span class=\"hljs-string\">'From'<\/span> =&gt; <span class=\"hljs-string\">'john.doe@example.com'<\/span>,\r\n    <span class=\"hljs-string\">'Reply-To'<\/span> =&gt; <span class=\"hljs-string\">'john.doe@example.com'<\/span>,\r\n    <span class=\"hljs-string\">'X-Mailer'<\/span> =&gt; <span class=\"hljs-string\">'PHP\/'<\/span> . phpversion()\r\n];\r\n\r\n\r\nmail($to, $subject, $message,  $headers);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='3-using-the-php-mail-function-to-send-html-email-example'>3) Using the PHP mail() function to send HTML email example <a href=\"#3-using-the-php-mail-function-to-send-html-email-example\" class=\"anchor\" id=\"3-using-the-php-mail-function-to-send-html-email-example\" title=\"Anchor for 3) Using the PHP mail() function to send HTML email example\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>To send HTML mail, you need to set the <code>Content-type<\/code> for the header like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n$to      = <span class=\"hljs-string\">'contact@phptutorial.net'<\/span>;\r\n$subject = <span class=\"hljs-string\">'This is a test email'<\/span>;\r\n$message = <span class=\"hljs-string\">'&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Email&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n    &lt;h1&gt;This is HTML mail&lt;\/h1&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;'<\/span>;\r\n\r\n\r\n$headers = &#91;\r\n    <span class=\"hljs-string\">'MIME-Version'<\/span> =&gt; <span class=\"hljs-string\">'1.0'<\/span>,\r\n    <span class=\"hljs-string\">'Content-type'<\/span> =&gt; <span class=\"hljs-string\">'text\/html; charset=utf8'<\/span>,\r\n    <span class=\"hljs-string\">'From'<\/span> =&gt; <span class=\"hljs-string\">'john.doe@example.com'<\/span>,\r\n    <span class=\"hljs-string\">'Reply-To'<\/span> =&gt; <span class=\"hljs-string\">'john.doe@example.com'<\/span>,\r\n    <span class=\"hljs-string\">'X-Mailer'<\/span> =&gt; <span class=\"hljs-string\">'PHP\/'<\/span> . phpversion()\r\n];\r\n\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (mail($to, $subject, $message,  $headers)) {\r\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'email was sent.'<\/span>;\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'An error occurred.'<\/span>;\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Use the PHP <code>mail()<\/code> function to send an email message.<\/li>\r\n<\/ul>\r\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2431\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-mail\/\"\n\t\t\t\tdata-post-title=\"PHP mail\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2431\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-mail\/\"\n\t\t\t\tdata-post-title=\"PHP mail\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the PHP mail() function to send email messages in plain text and HTML formats.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":78,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2431","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2431"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2431\/revisions"}],"predecessor-version":[{"id":3149,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2431\/revisions\/3149"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}