Blog de programación, errores, soluciones

Chose Language:
comments
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“localhost”PHP_INI_ALL 
smtp_port“25”PHP_INI_ALLDisponible a partir de PHP 4.3.0.
sendmail_fromNULLPHP_INI_ALL 
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:

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

$to

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 “destinatario@fakemail.com”

or

$to= "<destinatario@fakemail.com>";

The other case is when you have multiples destinataries

$to= "destinatario1@fakemail.com, destinatary2@fakemail.com, detinatary3@otroemail.com";

$subject

Email title (Must comply with RFC 2047)

$subject = "Clases suspendidas";

$message

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.

<?php
$message = str_replace("\n.", "\n..", $message);
?>

$additional_headers

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 no-reply@google.com.

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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$additional_headers .= 'From: Recordatorio <cumples@example.com>' . "\r\n";
$additional_headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$additional_headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

$additional_parameters

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 php.net that shows we may send emails with HTML format.

<?php
// Varios destinatarios
$para  = 'aidan@example.com' . ', '; // atención a la coma
$para .= 'wez@example.com';

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

// mensaje
$mensaje = '
<html>
<head>
  <title>Recordatorio de cumpleaños para Agosto</title>
</head>
<body>
  <p>¡Estos son los cumpleaños para Agosto!</p>
  <table>
    <tr>
      <th>Quien</th><th>Día</th><th>Mes</th><th>Año</th>
    </tr>
    <tr>
      <td>Joe</td><td>3</td><td>Agosto</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17</td><td>Agosto</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// 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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$cabeceras .= 'From: Recordatorio <cumples@example.com>' . "\r\n";
$cabeceras .= 'Cc: birthdayarchive@example.com' . "\r\n";
$cabeceras .= 'Bcc: birthdaycheck@example.com' . "\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

Comments