{"id":54144,"date":"2026-11-21T22:05:42","date_gmt":"2026-11-21T22:05:42","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=54144"},"modified":"2026-11-21T22:05:42","modified_gmt":"2026-11-21T22:05:42","slug":"php-code-for-godaddy-vps-server-smtp-email-relay","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-code-for-godaddy-vps-server-smtp-email-relay\/","title":{"rendered":"PHP Code for Godaddy VPS Server SMTP Email Relay"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This page shows how to send email from a GoDaddy VPS through GoDaddy&#039;s SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below the original script you will find what each SMTP step does, a corrected and safer version, how to test port 25, a PHPMailer alternative for production, and fixes for the errors people hit most often.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.godaddy.com\/en-in\/help\/find-the-smtp-relay-server-for-my-vps-hosting-24736\" rel=\"noopener\">https:\/\/www.godaddy.com\/en-in\/help\/find-the-smtp-relay-server-for-my-vps-hosting-24736<\/a><\/li>\n<\/ul>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/c94668117f250929d601df199c58feba.js\"><\/script>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"><\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the example script talks to the relay<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gist above opens a raw TCP connection with <code>fsockopen()<\/code> and has a full SMTP conversation by hand. GoDaddy&#039;s VPS relay does not ask for a username or password. It accepts mail based on the IP address the connection comes from, so the script never sends <code>AUTH<\/code>. Each command must get the expected reply code before the next one is sent:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Step<\/th><th>Client sends<\/th><th>Expected reply<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>Greeting<\/td><td>(nothing, just connect)<\/td><td>220<\/td><td>Server is ready<\/td><\/tr><tr><td>EHLO<\/td><td><code>EHLO your-hostname<\/code><\/td><td>250 (multi-line)<\/td><td>Server lists the extensions it supports<\/td><\/tr><tr><td>Envelope sender<\/td><td><code>MAIL FROM:&lt;sender&gt;<\/code><\/td><td>250<\/td><td>Sender accepted<\/td><\/tr><tr><td>Envelope recipient<\/td><td><code>RCPT TO:&lt;recipient&gt;<\/code><\/td><td>250 or 251<\/td><td>Recipient accepted for relay<\/td><\/tr><tr><td>DATA<\/td><td><code>DATA<\/code><\/td><td>354<\/td><td>Send headers and body, then end with a line holding a single dot<\/td><\/tr><tr><td>End of message<\/td><td><code>&lt;CRLF&gt;.&lt;CRLF&gt;<\/code><\/td><td>250<\/td><td>Message queued for delivery<\/td><\/tr><tr><td>QUIT<\/td><td><code>QUIT<\/code><\/td><td>221<\/td><td>Connection closed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>smtp_read_all()<\/code> function handles multi-line replies. A line such as <code>250-SIZE<\/code> has a hyphen in the fourth position, which means more lines are coming. The last line has a space there, for example <code>250 HELP<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before you run it: find the relay host and check port 25<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the GoDaddy help article linked above and copy the relay hostname it gives for your VPS plan and data centre. The hostname in the gist is one example. Do not assume it matches your server.<\/li>\n\n\n<li>Check that your VPS can reach that host on port 25. If this times out, nothing in PHP will work.<\/li>\n\n\n<li>On cPanel\/WHM servers, check <strong>WHM &gt; Security Center &gt; SMTP Restrictions<\/strong>. When it is on, ordinary users cannot make outbound SMTP connections, and PHP scripts running as a cPanel user are one of them.<\/li>\n\n\n<li>If CSF firewall is installed, check <code>SMTP_BLOCK<\/code> and <code>SMTP_ALLOWUSER<\/code> in <code>\/etc\/csf\/csf.conf<\/code>. Also check any <code>iptables<\/code>\/<code>firewalld<\/code> rules that block outbound port 25.<\/li>\n<\/ol>\n\n\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\"><span class=\"hljs-comment\"># Replace RELAY_HOST with the hostname from GoDaddy's article<\/span>\nnc -vz RELAY_HOST <span class=\"hljs-number\">25<\/span>\n\n<span class=\"hljs-comment\"># Or talk to it interactively (type QUIT to exit)<\/span>\ntelnet RELAY_HOST <span class=\"hljs-number\">25<\/span>\n<span class=\"hljs-comment\"># Expected first line: 220 ... ESMTP ...<\/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>\n\n\n<h2 class=\"wp-block-heading\">Corrected and hardened version of the script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This version keeps the same approach but fixes the problems listed in the corrections. Server output is escaped so it shows correctly in a browser. Reads time out instead of hanging. The message gets the required <code>Date<\/code> and <code>Message-ID<\/code> headers, the body is dot-stuffed, and the script stops with an error code at the first failed step.<\/p>\n\n\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\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ smtp-test.php  - run with: php smtp-test.php<\/span>\n$host = getenv(<span class=\"hljs-string\">'SMTP_RELAY_HOST'<\/span>) ?: <span class=\"hljs-string\">'n1smtpout.europe.secureserver.net'<\/span>; <span class=\"hljs-comment\">\/\/ use the host from GoDaddy's article<\/span>\n$port = <span class=\"hljs-number\">25<\/span>;\n$from = <span class=\"hljs-string\">'sender@yourdomain.com'<\/span>;\n$to   = <span class=\"hljs-string\">'you@yourdomain.com'<\/span>;\n$heloName = gethostname() ?: <span class=\"hljs-string\">'localhost'<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">out<\/span><span class=\"hljs-params\">(string $s)<\/span>: <span class=\"hljs-title\">void<\/span> <\/span>{\n    <span class=\"hljs-keyword\">echo<\/span> PHP_SAPI === <span class=\"hljs-string\">'cli'<\/span> ? $s : htmlspecialchars($s, ENT_QUOTES, <span class=\"hljs-string\">'UTF-8'<\/span>);\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">smtp_read<\/span><span class=\"hljs-params\">($fp)<\/span>: <span class=\"hljs-title\">array<\/span> <\/span>{\n    $response = <span class=\"hljs-string\">''<\/span>;\n    $code = <span class=\"hljs-keyword\">null<\/span>;\n    <span class=\"hljs-keyword\">while<\/span> (($line = fgets($fp, <span class=\"hljs-number\">1024<\/span>)) !== <span class=\"hljs-keyword\">false<\/span>) {\n        out(<span class=\"hljs-string\">\"&lt;&lt; $line\"<\/span>);\n        $response .= $line;\n        <span class=\"hljs-keyword\">if<\/span> (strlen($line) &lt; <span class=\"hljs-number\">4<\/span> || !ctype_digit(substr($line, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">3<\/span>))) {\n            <span class=\"hljs-keyword\">break<\/span>;\n        }\n        $code = (int) substr($line, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">3<\/span>);\n        <span class=\"hljs-keyword\">if<\/span> ($line&#91;<span class=\"hljs-number\">3<\/span>] === <span class=\"hljs-string\">' '<\/span>) {\n            <span class=\"hljs-keyword\">break<\/span>; <span class=\"hljs-comment\">\/\/ last line of a (possibly multi-line) reply<\/span>\n        }\n    }\n    <span class=\"hljs-keyword\">if<\/span> (stream_get_meta_data($fp)&#91;<span class=\"hljs-string\">'timed_out'<\/span>]) {\n        out(<span class=\"hljs-string\">\"!! Timed out waiting for the server\\n\"<\/span>);\n    }\n    <span class=\"hljs-keyword\">return<\/span> &#91;$code, $response];\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">smtp_cmd<\/span><span class=\"hljs-params\">($fp, string $cmd)<\/span>: <span class=\"hljs-title\">array<\/span> <\/span>{\n    out(<span class=\"hljs-string\">\"&gt;&gt; $cmd\\n\"<\/span>);\n    fwrite($fp, $cmd . <span class=\"hljs-string\">\"\\r\\n\"<\/span>);\n    <span class=\"hljs-keyword\">return<\/span> smtp_read($fp);\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">expect<\/span><span class=\"hljs-params\">($fp, ?int $code, array $ok, string $step)<\/span>: <span class=\"hljs-title\">void<\/span> <\/span>{\n    <span class=\"hljs-keyword\">if<\/span> (!in_array($code, $ok, <span class=\"hljs-keyword\">true<\/span>)) {\n        out(<span class=\"hljs-string\">\"$step failed, reply code: \"<\/span> . var_export($code, <span class=\"hljs-keyword\">true<\/span>) . <span class=\"hljs-string\">\"\\n\"<\/span>);\n        fwrite($fp, <span class=\"hljs-string\">\"QUIT\\r\\n\"<\/span>);\n        fclose($fp);\n        <span class=\"hljs-keyword\">exit<\/span>(<span class=\"hljs-number\">1<\/span>);\n    }\n}\n\n<span class=\"hljs-keyword\">if<\/span> (PHP_SAPI !== <span class=\"hljs-string\">'cli'<\/span>) { <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;pre&gt;'<\/span>; }\nout(<span class=\"hljs-string\">\"Connecting to $host:$port...\\n\"<\/span>);\n\n$fp = @fsockopen($host, $port, $errno, $errstr, <span class=\"hljs-number\">20<\/span>); <span class=\"hljs-comment\">\/\/ 20 s connect timeout<\/span>\n<span class=\"hljs-keyword\">if<\/span> (!$fp) {\n    out(<span class=\"hljs-string\">\"Connection failed: $errno - $errstr\\n\"<\/span>);\n    <span class=\"hljs-keyword\">exit<\/span>(<span class=\"hljs-number\">1<\/span>);\n}\nstream_set_timeout($fp, <span class=\"hljs-number\">30<\/span>); <span class=\"hljs-comment\">\/\/ read timeout for every reply<\/span>\n\n&#91;$code] = smtp_read($fp);                       expect($fp, $code, &#91;<span class=\"hljs-number\">220<\/span>], <span class=\"hljs-string\">'Greeting'<\/span>);\n&#91;$code] = smtp_cmd($fp, <span class=\"hljs-string\">\"EHLO $heloName\"<\/span>);      expect($fp, $code, &#91;<span class=\"hljs-number\">250<\/span>], <span class=\"hljs-string\">'EHLO'<\/span>);\n&#91;$code] = smtp_cmd($fp, <span class=\"hljs-string\">\"MAIL FROM:&lt;$from&gt;\"<\/span>);   expect($fp, $code, &#91;<span class=\"hljs-number\">250<\/span>], <span class=\"hljs-string\">'MAIL FROM'<\/span>);\n&#91;$code] = smtp_cmd($fp, <span class=\"hljs-string\">\"RCPT TO:&lt;$to&gt;\"<\/span>);       expect($fp, $code, &#91;<span class=\"hljs-number\">250<\/span>, <span class=\"hljs-number\">251<\/span>], <span class=\"hljs-string\">'RCPT TO'<\/span>);\n&#91;$code] = smtp_cmd($fp, <span class=\"hljs-string\">'DATA'<\/span>);                expect($fp, $code, &#91;<span class=\"hljs-number\">354<\/span>], <span class=\"hljs-string\">'DATA'<\/span>);\n\n$domain = substr(strrchr($from, <span class=\"hljs-string\">'@'<\/span>), <span class=\"hljs-number\">1<\/span>);\n$headers = &#91;\n    <span class=\"hljs-string\">'Date: '<\/span> . date(<span class=\"hljs-string\">'r'<\/span>),\n    <span class=\"hljs-string\">'From: '<\/span> . $from,\n    <span class=\"hljs-string\">'To: '<\/span> . $to,\n    <span class=\"hljs-string\">'Subject: GoDaddy SMTP relay test'<\/span>,\n    <span class=\"hljs-string\">'Message-ID: &lt;'<\/span> . bin2hex(random_bytes(<span class=\"hljs-number\">12<\/span>)) . <span class=\"hljs-string\">'@'<\/span> . $domain . <span class=\"hljs-string\">'&gt;'<\/span>,\n    <span class=\"hljs-string\">'MIME-Version: 1.0'<\/span>,\n    <span class=\"hljs-string\">'Content-Type: text\/plain; charset=UTF-8'<\/span>,\n];\n$body = <span class=\"hljs-string\">\"This is a test email sent through the GoDaddy relay on port 25.\\n\"<\/span>\n      . <span class=\"hljs-string\">\"If it reaches your inbox, SMTP works from this server.\\n\"<\/span>;\n\n$data = implode(<span class=\"hljs-string\">\"\\r\\n\"<\/span>, $headers) . <span class=\"hljs-string\">\"\\r\\n\\r\\n\"<\/span> . $body;\n$data = preg_replace(<span class=\"hljs-string\">\"\/\\r\\n|\\r|\\n\/\"<\/span>, <span class=\"hljs-string\">\"\\r\\n\"<\/span>, $data); <span class=\"hljs-comment\">\/\/ SMTP requires CRLF line endings<\/span>\n$data = preg_replace(<span class=\"hljs-string\">'\/^\\.\/m'<\/span>, <span class=\"hljs-string\">'..'<\/span>, $data);        <span class=\"hljs-comment\">\/\/ dot-stuffing (RFC 5321)<\/span>\n\nout(<span class=\"hljs-string\">\"&gt;&gt; &#91;message headers and body]\\n\"<\/span>);\nfwrite($fp, rtrim($data, <span class=\"hljs-string\">\"\\r\\n\"<\/span>) . <span class=\"hljs-string\">\"\\r\\n.\\r\\n\"<\/span>);\n&#91;$code] = smtp_read($fp);                       expect($fp, $code, &#91;<span class=\"hljs-number\">250<\/span>], <span class=\"hljs-string\">'Message'<\/span>);\n\nsmtp_cmd($fp, <span class=\"hljs-string\">'QUIT'<\/span>);\nfclose($fp);\nout(<span class=\"hljs-string\">\"\\nDone: message accepted by the relay.\\n\"<\/span>);\n<span class=\"hljs-keyword\">if<\/span> (PHP_SAPI !== <span class=\"hljs-string\">'cli'<\/span>) { <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;\/pre&gt;'<\/span>; }<\/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>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The short array destructuring <code>[$code] = ...<\/code> and the typed parameters need PHP 7.1 or newer. Every supported PHP release meets this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running the test and reading the output<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run the script from the shell on the VPS rather than through a browser. You see the raw output, and nobody else can trigger it. Set the relay host with an environment variable so it stays out of the code:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SMTP_RELAY_HOST=RELAY_HOST php smtp-test.php<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\">A successful run looks roughly like this. The server text after each code changes from server to server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">Connecting<\/span> <span class=\"hljs-selector-tag\">to<\/span> <span class=\"hljs-selector-tag\">RELAY_HOST<\/span><span class=\"hljs-selector-pseudo\">:25...<\/span>\n&lt;&lt; 220 ... <span class=\"hljs-selector-tag\">ESMTP<\/span> ...\n&gt;&gt; <span class=\"hljs-selector-tag\">EHLO<\/span> <span class=\"hljs-selector-tag\">vps<\/span><span class=\"hljs-selector-class\">.yourdomain<\/span><span class=\"hljs-selector-class\">.com<\/span>\n&lt;&lt; 250<span class=\"hljs-selector-tag\">-<\/span>...\n&lt;&lt; 250 ...\n&gt;&gt; <span class=\"hljs-selector-tag\">MAIL<\/span> <span class=\"hljs-selector-tag\">FROM<\/span>:&lt;<span class=\"hljs-selector-tag\">sender<\/span><span class=\"hljs-keyword\">@yourdomain<\/span>.com&gt;\n&lt;&lt; <span class=\"hljs-number\">250<\/span> ...\n&gt;&gt; RCPT <span class=\"hljs-attribute\">TO:<\/span>&lt;you@yourdomain.com&gt;\n&lt;&lt; <span class=\"hljs-number\">250<\/span> ...\n&gt;&gt; DATA\n&lt;&lt; <span class=\"hljs-number\">354<\/span> ...\n&gt;&gt; &#91;message headers <span class=\"hljs-keyword\">and<\/span> body]\n&lt;&lt; <span class=\"hljs-number\">250<\/span> ... queued ...\n&gt;&gt; QUIT\n&lt;&lt; <span class=\"hljs-number\">221<\/span> ...\n\n<span class=\"hljs-attribute\">Done:<\/span> message accepted by the relay.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">A final <code>250<\/code> only means the relay accepted the message. Whether it reaches the inbox depends on the recipient&#039;s spam filtering, so check the spam folder as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use PHPMailer for real applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A hand-written socket client is fine for testing. For application mail, use a maintained library. It handles encoding, attachments, HTML bodies, header injection protection and errors for you. Install PHPMailer with Composer (<code>composer require phpmailer\/phpmailer<\/code>) and point it at the same relay without authentication:<\/p>\n\n\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>\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">PHPMailer<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">SMTP<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">PHPMailer<\/span>\\<span class=\"hljs-title\">Exception<\/span>;\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/vendor\/autoload.php'<\/span>;\n\n$mail = <span class=\"hljs-keyword\">new<\/span> PHPMailer(<span class=\"hljs-keyword\">true<\/span>); <span class=\"hljs-comment\">\/\/ throw exceptions on errors<\/span>\n<span class=\"hljs-keyword\">try<\/span> {\n    $mail-&gt;isSMTP();\n    $mail-&gt;Host        = getenv(<span class=\"hljs-string\">'SMTP_RELAY_HOST'<\/span>); <span class=\"hljs-comment\">\/\/ from GoDaddy's article<\/span>\n    $mail-&gt;Port        = <span class=\"hljs-number\">25<\/span>;\n    $mail-&gt;SMTPAuth    = <span class=\"hljs-keyword\">false<\/span>;  <span class=\"hljs-comment\">\/\/ relay authorises by server IP<\/span>\n    $mail-&gt;SMTPAutoTLS = <span class=\"hljs-keyword\">false<\/span>;  <span class=\"hljs-comment\">\/\/ set true if the relay offers STARTTLS with a valid certificate<\/span>\n    $mail-&gt;SMTPDebug   = SMTP::DEBUG_OFF; <span class=\"hljs-comment\">\/\/ use SMTP::DEBUG_SERVER while troubleshooting<\/span>\n    $mail-&gt;Timeout     = <span class=\"hljs-number\">30<\/span>;\n\n    $mail-&gt;setFrom(<span class=\"hljs-string\">'sender@yourdomain.com'<\/span>, <span class=\"hljs-string\">'Your App'<\/span>);\n    $mail-&gt;addAddress(<span class=\"hljs-string\">'you@yourdomain.com'<\/span>);\n    $mail-&gt;Subject = <span class=\"hljs-string\">'GoDaddy relay test via PHPMailer'<\/span>;\n    $mail-&gt;Body    = <span class=\"hljs-string\">\"Hello from PHPMailer.\\n\"<\/span>;\n\n    $mail-&gt;send();\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Sent\\n\"<\/span>;\n} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-keyword\">Exception<\/span> $e) {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Mailer error: {$mail-&gt;ErrorInfo}\\n\"<\/span>;\n}<\/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>\n\n\n<p class=\"wp-block-paragraph\">Frameworks such as Laravel, Symfony and CodeIgniter have their own mail settings that take the same values: host, port 25, no username or password, no encryption.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Alternative: relay PHP mail() through Postfix<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If existing code uses PHP&#039;s built-in <code>mail()<\/code>, you can leave it alone and have the local MTA forward everything to GoDaddy instead. With Postfix installed, set the relay host and reload:<\/p>\n\n\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\">sudo postconf -e <span class=\"hljs-string\">'relayhost = &#91;RELAY_HOST]:25'<\/span>\nsudo systemctl reload postfix\n\n<span class=\"hljs-comment\"># Send a test and watch the log<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Test body\"<\/span> | mail -s <span class=\"hljs-string\">\"Postfix relay test\"<\/span> you@yourdomain.com\nsudo tail -f \/<span class=\"hljs-keyword\">var<\/span>\/log\/mail.log   <span class=\"hljs-comment\"># \/var\/log\/maillog on RHEL-family systems<\/span><\/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>\n\n\n<p class=\"wp-block-paragraph\">The square brackets tell Postfix to connect to that host directly and skip the MX lookup. In the log, look for <code>status=sent<\/code> with <code>relay=<\/code> showing the GoDaddy host.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>Connection timed out (110)<\/code><\/td><td>Outbound port 25 blocked by WHM SMTP Restrictions, CSF <code>SMTP_BLOCK<\/code> or another firewall, or wrong host<\/td><td>Test with <code>nc -vz<\/code>; allow the PHP user or turn off the restriction; confirm the hostname<\/td><\/tr><tr><td><code>Connection refused (111)<\/code><\/td><td>Wrong host or port<\/td><td>Use the exact relay host and port from GoDaddy&#039;s article<\/td><\/tr><tr><td><code>getaddrinfo failed<\/code><\/td><td>Hostname typo or DNS problem on the VPS<\/td><td>Check spelling; test with <code>dig RELAY_HOST<\/code><\/td><\/tr><tr><td><code>550<\/code> or <code>554<\/code> after MAIL FROM or RCPT TO<\/td><td>Relay refuses this server&#039;s IP or sender<\/td><td>Confirm the VPS is a GoDaddy-hosted server allowed to use that relay; use a sender on your own domain<\/td><\/tr><tr><td><code>421<\/code> or <code>4xx<\/code> replies<\/td><td>Temporary limit or service problem<\/td><td>Retry later; queue mail instead of sending in bursts<\/td><\/tr><tr><td>Script hangs with no output<\/td><td>No read timeout on the socket<\/td><td>Use <code>stream_set_timeout()<\/code> as in the corrected script<\/td><\/tr><tr><td>Final 250 but mail never arrives<\/td><td>Spam filtering, SPF\/DMARC failure<\/td><td>Check spam; publish SPF that authorises the relay; check message headers at the recipient<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Security and deliverability notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Do not leave a test mailer in the web root. Anyone who finds the URL can send mail as your domain. Run it from the CLI, or delete it after testing.<\/li>\n\n\n<li>Never put user input straight into headers (<code>To<\/code>, <code>Subject<\/code>, <code>From<\/code>). A CR or LF in that input lets an attacker add headers or recipients. Validate addresses with <code>filter_var($addr, FILTER_VALIDATE_EMAIL)<\/code> or use PHPMailer.<\/li>\n\n\n<li>Port 25 relay traffic to GoDaddy is usually unencrypted. Do not send passwords or sensitive data in the message body.<\/li>\n\n\n<li>Publish an SPF record for your sending domain that includes GoDaddy&#039;s mail servers (GoDaddy commonly documents <code>include:secureserver.net<\/code>). Add DMARC to improve inbox placement.<\/li>\n\n\n<li>Use a <code>From<\/code> address on a domain you control. Sending as Gmail or another provider&#039;s domain will fail their DMARC checks.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What SMTP server should I use on a GoDaddy VPS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the relay hostname in GoDaddy&#039;s help article for VPS hosting (linked at the top of this post). It can depend on your plan and data centre. The relay runs on port 25 and authorises your server by IP address, so no username or password is needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does fsockopen time out when connecting to the GoDaddy relay?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Usually something is blocking outbound port 25. Common causes are WHM&#039;s SMTP Restrictions, CSF&#039;s SMTP_BLOCK setting or a local firewall rule. Check with <code>nc -vz RELAY_HOST 25<\/code> from the server. If that also times out, fix the network side before you debug PHP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use smtpout.secureserver.net with a username and password instead?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">That host is for authenticated mailboxes hosted with GoDaddy, not the IP-based VPS relay. If you have a GoDaddy mailbox, you can authenticate to it with PHPMailer by setting SMTPAuth to true and supplying the mailbox credentials and the port GoDaddy documents for it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The script says the message was accepted but no email arrived. Why?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A 250 reply only means the relay queued the message. Check the spam folder, make sure the From address uses a domain you own, and publish SPF and DMARC records that authorise GoDaddy&#039;s servers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use raw sockets or PHPMailer?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use raw sockets only to test connectivity and see the SMTP conversation. For application mail, use PHPMailer or your framework&#039;s mailer. They handle encoding, attachments and header safety for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/programming\/php\/\">More PHP tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-codeigniter-and-use-cases-of-codeigniter\/\">What is CodeIgniter and use cases of CodeIgniter?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/complete-tutorials-of-php-oop-classes-objects-with-example-code\/\">Complete Tutorials of PHP OOP Classes\/Objects with Example code<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-php-interview-questions-and-answers\/\">Top 50 PHP Interview Questions and Answers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This page shows how to send email from a GoDaddy VPS through GoDaddy&#039;s SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[488],"class_list":["post-54144","post","type-post","status-publish","format-standard","hentry","category-php","tag-smtp"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/54144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=54144"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/54144\/revisions"}],"predecessor-version":[{"id":80103,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/54144\/revisions\/80103"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=54144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=54144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=54144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}