1
send email php
CGuild1.com > Tips & Tricks > send email php

How to Send an Email Using PHP

This is a raw script on sending an email using php. Typically I would either post something to the script and insert into a database, this is however just the send mail itself, html formatted.

<?php
    //Single reciepent
    $to = 'test@example.com';
    
    /* multiple recipients
    $to = 'aidan@example.com' . ', '; // note the comma
    $to .= 'wez@example.com';
    */
    // subject
    $subject = 'The Subject';
    
    // message
    $message = '
        <html>
        <head>
         <title>The title of the html file</title>
        </head>
        <body>
         <p>Here is the test email</p>
         <table>
            <tr>
             <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
            </tr>
            <tr>
             <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
            </tr>
            <tr>
             <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
            </tr>
         </table>
        </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
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: The tester <thetester@example.com>' . "\r\n";
    $headers .= 'Cc: thetester1@example.com' . "\r\n";
    $headers .= 'Bcc: thetester2@example.com' . "\r\n";
    
    // Mail it
    mail($to, $subject, $message, $headers);

//What to do after the email is sent
    header('Location: http://www.example.com/thetestemailsent/');
?>
You can read more about it on stackoverflow here.
©2024 CGuild1.com