PHP mail() Function

PHP send email messages are very common for a web application for example, send welcome email when a user create an account on your website, sending newsletters to your registered.

PHP create use of mail() function to send an email. PHP mail function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.


mail( to, subject, message, headers, parameters );

Here is the description for each parameters.

> to : It specifies the receiver / receivers of the email

> subject : It specifies the subject of the email

> message : It defines the message to be sent

> headers : It specifies additional headers, like From, Cc, and Bcc

> parameters : Used to pass additional parameters

Sending Plain Text Emails

The simple way to send an email with PHP is to send a text email. In the example below we first declare the variables recipient's email address, subject line and message body then we pass these variables to the mail() function to send the email.


    $to = '';
    $subject = 'Hi';
    $message = 'This is a test mail...!'; 
    $from = '';

    // Sending email
    if(mail($to, $subject, $message)){
        echo 'Your mail has been sent successfully...';
    } else{
        echo 'Sorry, unable to send mail...';