Sending Email From PHP On GoDaddy (And Possibly Other Hosts)

Recently, while working on the temporary site for Moonfar, I wanted to send the user an email as confirmation that they had signed up for email notifications. Essentially, you define the recipient, the subject line, the body, a few headers, and then call the mail() function while passing those parameters to it. PHP's mail() function does the rest, provided that your host has PHP configured properly.

Let me preface this by saying that I've only tested this on GoDaddy's Linux Deluxe Hosting plan. If you are trying to do this with another web host, you need to make sure that the install of PHP you are using supports the mail() function.

<?php
  $to = 'user@theirdomain.com';
  $subject = 'Your subject line';

  // the message here is HTML, but you could
  // use plain text in the same manner
  // this could also be pulled from a template file
  $message = '
    <html>
    <head>
      <title>Your Title Here</title>
    </head>
    <body>
      <p>Your content here...</p>
    </body>
    </html>';

  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  // Additional headers
  // I'm not sure how the To: field in the header functions since it's part
  // of the function call, so I've commented it out here
  //$headers .= 'To: ' . $emailAddr . "\r\n";
  $headers .= 'From: YourName <you@yourdomain.com>' . "\r\n";
  $headers .= 'Cc: ' . "\r\n";
  // if you want to receive a copy
  $headers .= 'Bcc: you@yourdomain.com' . "\r\n";

  // Mail it
  mail($to, $subject, $message, $headers);
?>

It's as easy as that. That said, I read somewhere that there is a 1000 emails per day limit using this method on GoDaddy without paying for outbound emailing services, but I could be mistaken.

Category: 

Comments

You are correct, it is a 1,000 email limit on GoDaddy.

Add new comment

You can optionally provide your email address, which will remain private, if you wish to be contacted directly about this comment.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.