Correct form
To send an Email With Hidden Copy (CCO) or English Blind Carbon Copy (BCC), simply add it in the header of the email
the following statement
$emailoculto = '[email protected]';
$cabecalho .= "Bcc: {$emailoculto}\r\n";
Attention: According to the documentation of the mail()
function in PHP , multiple headers must be separated with CRLF
or be \r\n
:
Multiple extra headers must be separated with a CRLF (\ r \ n)
Shape not so correct
You can also do with POG. This alternative consists in sending the email twice, the first one to its recipients and a second time for the hidden copy:
$emailoculto = '[email protected]';
$status_envio = @mail ($email_destinatario, $assunto, $mensagem, $cabecalho);
if ($status_envio) { // Se mensagem foi enviada pelo servidor...
echo "Uma menságem foi enviada para o e-mail do usuário!";
// Envia para o destinatário oculto
if (!mail($emailoculto, $assunto, $mensagem, $cabecalho))
echo "!";
echo "<br />";
} else // Se mensagem não foi enviada pelo servidor...
echo "Não conseguimos enviar a sua mensagem ao usuário!<br />";
Best Form
You can also (or should) use the class PHPMailer to send your emails.
This is the best alternative for sending emails with PHP, since you have several features in a very simple way: add attachments, multiple recipients, copies or hidden copies .
In addition to being able to easily configure SMTP sending, some servers do not support the mail()
function.
Example usage:
email.php
require '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 = '[email protected]'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, 'ssl' also accepted
$mail->Port = 587;
send.php
require_once 'email.php'
global $mail;
$mail->From = '[email protected]'; // DE
$mail->FromName = 'Mailer'; // DE Nome
$mail->addAddress('[email protected]', 'Joe User'); // Destinatário
$mail->addAddress('[email protected]'); // Nome OPCIONAL
$mail->addReplyTo('[email protected]', 'Information'); // Responder Para
$mail->addCC('[email protected]'); // CÓPIA
$mail->addBCC('[email protected]'); // CÓPIA OCULTA
$mail->addAttachment('/var/tmp/file.tar.gz'); // Adicionar Anexo
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Nome OPCIONAL
$mail->isHTML(true); // E-mail no formato HTML
$mail->Subject = 'Here is the subject'; // TÍTULO
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; // Corpo HTML
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // Corpo TEXTO
if($mail->send()) {
echo 'Mensagem enviada com sucesso.';
} else {
echo 'Erro ao tentar enviar mensagem.<br>';
echo 'Erro: ' . $mail->ErrorInfo;
}