PHP Sending E-mails

Posted by tutor | Posted in PHP Tutorial | Posted on 15-12-2009-05-2008

0

PHP allows you to send e-mails directly from a script.

The PHP mail() Function
The PHP mail() function is used to send emails from inside a script.

Syntax

    mail(to,subject,message,headers,parameters)
    Parameter Description
    to Required. Specifies the receiver / receivers of the email
    subject Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
    message Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
    headers Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
    parameters Optional. Specifies an additional parameter to the sendmail program

But before you will be able to use mail functions, you should have a working email system. The program to be used is defined by the configuration settings in the php.ini file.

PHP Simple E-Mail

In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:

    $to = "user1@example.com";
    $subject = "Sample mail";
    $message = "This is a test email message.";
    $from = "user2@example.com";
    $headers = "From: $from";
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
    ?>

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.