Send e-mail via PHP

Some popular examples for sending emails via PHP.
Example #1 “Small and common” (Just text) ↓

$customer_email = 'customer@example.com';
$email_subject = 'Test e-mail subject';
$email_message = "Hello, Customer\r\nIt's our message information\r\nThanks!";
mail( $customer_email, $email_subject, $email_message);

Example #2 “Simple with headers” (Just text) ↓

$site_email = 'noreply@example.com';
$customer_email = 'nobody@example.com';
$email_subject = 'Test e-mail subject';
$email_message = "Hello, Customer\r\nIt's our message information\r\nThanks!";
$headers =  'From: ' .$site_email. "\r\n" .
            'Reply-To:'.$site_email."\r\n";
mail($customer_email, $email_subject, $email_message, $headers);

OR (by only set headers manual) ↓

$site_name_email = 'WebSiteName';
$site_email = 'noreply@example.com';
$customer_email = 'nobody@example.com';
$email_subject = 'Test e-mail subject';
$email_message = "Hello, Customer\r\nIt's our message information\r\nThanks!";

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=UTF-8";
$headers[] = "From: {$site_name_email} <{$site_email}>";
$headers[] = "Bcc: <additional_email@example.com>";
$headers[] = "Reply-To: <{$site_email}>";
$headers[] = "Subject: {$email_subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($customer_email, $email_subject, $email_message, implode("\r\n", $headers));

Example #3 “Popular html version” ↓

$site_name_email = 'WebSiteName';
$customer_name = 'John Snow';
$site_email = 'noreply@example.com';
$customer_email = 'nobody@example.com';
$email_subject = 'Test e-mail subject';
$email_message = '<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'.$email_subject.'</title></head>
<body>
   <h2>'.$email_subject.'</h2>
   <hr />
   <div>
        <p>Hello, <strong>'.$customer_name.'</strong></p>
        <p>It\'s our message information</p>
        <p>Thanks!</p>
   </div>
   <hr />
   Regards, '.$site_name_email.'<br/>
   <small>Please DON\'T reply on this message! '.date('Y-m-d').'</small>
</body>
</html>';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=UTF-8";
$headers[] = "From: {$site_name_email} <{$site_email}>";
$headers[] = "Cc: <additional_email@example.com>";
$headers[] = "Bcc: <additional_email@example.com>";
$headers[] = "Reply-To: <{$site_email}>";
$headers[] = "Subject: {$email_subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($customer_email, $email_subject, $email_message, implode("\r\n", $headers));

If your emails will include a complex structure or attachments then better to use one of specialized libraries for sending emails.
One of the most popular – is phpMailer.
Example “Send email message with attachments via phpMailer and use SMTP server

<?php
require_once 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}