Correct form
To send an email With Hidden Copy (CCO) or Blind Carbon Copy (BCC), simply add to the email header
the following instruction
$emailoculto = '[email protected]';
$cabecalho .= "Bcc: {$emailoculto}\r\n";
Heed: According to the job documentation mail()
in PHP, multiple headers should be separated with CRLF
that is to say \r\n
:
Multiple extra headers should be separated with a CRLF ( r n)
Not so correct shape
You can also do with POG. This alternative consists of sending the email twice, the first to its recipients and a second time to 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 />";
Better Form
You can also (or should) use the class Phpmailer to send your emails.
This is the best alternative to sending emails with PHP, as you have several features in a very simple way like: add attachments, multiple recipients, copies or hidden copies.
In addition to the possibility to easily configure the sending by SMTP, because some servers do not support function mail()
.
Example of use:
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;
}
Did any of the answers help you? Or are there problems with them? Comment informing the author who doubts to try to use the proposed solution. If any of the answers solved your problem, mark it as correct by clicking on
– Guilherme Nascimento