Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

Sending Emails with PHP

On this post I’m going to explain how to send Email with PHP, at first this post was thought to touch function mail() only, but this probably is not the best tool to send emails in your situation, so I’ll divide this post in 3, this and 2 other post writing about how to send email with PHPMailer and CURL.

Function mail()

To use mail() function you will need: sendmail, qmail, postfix or other SMTP installed, and refer this, you may make the reference in php.ini line sendmail

Para usar la función mail necesitarás, sendmail, qmail, postfix o otro sistema SMTP instalado y referenciar este, esto seria en el php.ini más precisamente en sendmail.

Nombre Por defecto Cambiable Historial de cambios
mail.add_x_header“0”PHP_INI_PERDIRDisponible a partir de PHP 5.3.0.
mail.logNULLPHP_INI_PERDIRDisponible a partir de PHP 5.3.0. (PHP_INI_SYSTEM|PHP_INI_PERDIR)
mail.force_extra_parametersNULLPHP_INI_PERDIRDisp since PHP 5.0.0. (PHP_INI_SYSTEM|PHP_INI_PERDIR)
smtp_port“25”PHP_INI_ALLDisponible a partir de PHP 4.3.0.
sendmail_path“/usr/sbin/sendmail -t -i”PHP_INI_SYSTEM 

the easiest way, to send mail with php is using mail() function, but as how we described before you need and SMTP or mail sender(sendmail) to use it.


Sintaxis / Sintax
mail( string $to, string $subject, string $message
 [, string $additional_headers [, string $additional_parameters ]] ) : bool


Who will recive the mail (must use format described in RFC 2822 – Internet Message Format)

Examples: 1 destinatary, fakemail is a ficticius name only for the example

In case of 1 destinatary you can use a string “”


$to= "<>";

The other case is when you have multiples destinataries

$to= ",,";


Email title (Must comply with RFC 2047)

$subject = "Clases suspendidas";


Message to be sent to the destinatary, every line must be separated with CRLF and must not use more than 70 characters.

¿What does mean to be separated with CRLF?

Well this mean new line \n and carrage return \r.

Example of $message :

$message  = "Este es un mensaje de bedelia para los estudiantes de 1er semestre informando\n";
$message .= "que no se tendran que presentar a clases el dia 20 de mayo\n.";

If you use Windows, you need to take in consideration that the point is erased, and you need to add one.

$message = str_replace("\n.", "\n..", $message);


This optional parameter is used normally to add extra headers, this could be (From, Bcc, Cc and others).

From: indicates who is sending the email, you may use a no-reply email to indicate the destinatary that not need to reply. These email are used by enterprise to communicate only. Example

CC: copy carbon, all destinataries can see the others you have sent the message.

Bcc: blind copy carbon, in case you send multiple copies the destinataries could not see the rest of destinataries.

$additional_headers .= 'To: Mary <>, Kelly <>' . "\r\n";
$additional_headers .= 'From: Recordatorio <>' . "\r\n";
$additional_headers .= 'Cc:' . "\r\n";
$additional_headers .= 'Bcc:' . "\r\n";


As the last one, this parameter is optional and can be used to indicate additional options that could be line command options, defined in sendmail_path configuration option.

Complete example

In this last section, we are going to see an example form that shows we may send emails with HTML format.

// Varios destinatarios
$para  = '' . ', '; // atención a la coma
$para .= '';

// título
$título = 'Recordatorio de cumpleaños para Agosto';

// mensaje
$mensaje = '
  <title>Recordatorio de cumpleaños para Agosto</title>
  <p>¡Estos son los cumpleaños para Agosto!</p>

// Para enviar un correo HTML, debe establecerse la cabecera Content-type
$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Cabeceras adicionales
$cabeceras .= 'To: Mary <>, Kelly <>' . "\r\n";
$cabeceras .= 'From: Recordatorio <>' . "\r\n";
$cabeceras .= 'Cc:' . "\r\n";
$cabeceras .= 'Bcc:' . "\r\n";

// Enviarlo
mail($para, $título, $mensaje, $cabeceras);
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category