Results 1 to 1 of 1

Thread: Using PHP to Send E-mail

  1. #1

    Default Using PHP to Send E-mail

    This is a little tutorial/examples on how to send email using PHP.
    PHP makes this very easy.

    This will work on both our Windows and Linux servers.

    The built-in function that does the job is:
    A simple example:
    PHP Code:
    "[email protected]";
    $subject "Test mail";

    //To add a line break in your message use \n 
    $message "Hello! This is a simple email message. \n This is line 2 in message \n now we're on line 3";

    // In case any of our lines are larger than 70 characters, we should use wordwrap()
    $message wordwrap($message70);

    $from "[email protected]";
    $headers "From: $from";

    "Mail Sent.";
    "An error has occurred, mail not sent";
    To retrieve submitted data from a HTML form using PHP:
    PHP Code:
    //To assign the text passed from an input field named "message" using the  "get" method:
    $variable $_GET['message'];
    //For POST Method
    $variable2 $_POST['message'];
    More advanced headers can be used if needed, example:
    PHP Code:
    $headers 'From: [email protected]"\r\n" .
    'Reply-To: [email protected]"\r\n" .
    'X-Mailer: PHP/' phpversion(); 
    If you want to send a HTML email, here is an example of one:
    PHP Code:
    // multiple recipients
    $to  '[email protected]', '// note the comma
    $to .= '[email protected]';

    // subject
    $subject 'Birthday Reminders for August';

    // message
    $message '
      <title>Birthday Reminders for August</title>
      <p>Here are the birthdays upcoming in August!</p>

    // 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
    $headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' "\r\n";
    $headers .= 'From: Birthday Reminder <[email protected]>' "\r\n";
    $headers .= 'Cc: [email protected]"\r\n";
    $headers .= 'Bcc: [email protected]"\r\n";

    // Mail it
    "Mail Sent.";
    "An error has occurred, mail not sent";
    Last edited by Max; September 29th, 2009 at 09:53 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts