Send email with CCO in PHP

Wanted to know how to send email with CCO ("with hidden copy")?

Formula Code:

require('config.php');
$sqlstt = "SELECT * FROM tb_site WHERE id='1'";
$resultstt = mysql_query($sqlstt);        
$rowtt = mysql_fetch_array($resultstt);

$sitename = $rowtt['sitename'];
$supmails = $rowtt['sitepp']; // E-mail Suporte.
$supmail  = ""; // Envio do email.
$bcc      = null;
$nome_remetente = "Email Marketing $sitename";   
$assunto = strtoupper($user).", E-Mail Marketing";
$email_remetente = $supmails;

// Inicio do envio de menságem para o usuário //    

$email_destinatario = $emaills;

// Conteudo do e-mail (voce poderá usar HTML) //
$mensagem = $description;

// Cabeçalho do e-mail. Nao é necessário alterar geralmente...
$cabecalho  = "MIME-Version: 1.0\n";
$cabecalho .= "Content-Type: text/html; charset=iso-8859-1\n";
$cabecalho .= "From: \"{$nome_remetente}\" <{$email_remetente}>\n";

// Dispara e-mail e retorna status para variável
$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!<br />";
} else { // Se mensagem nao foi enviada pelo servidor...
    echo "Nao conseguimos enviar a sua menságem ao usuário!<br />";
}
 8
php
Author: Pedro Sanção, 2014-07-16

3 answers

Correct form

To send a CCO or blind Carbon Copy (BCC) email, simply add in the email header the following statement

$emailoculto = '[email protected]';
$cabecalho .= "Bcc: {$emailoculto}\r\n";

Attention: according to the function documentation mail() in PHP , multiple headers must be separated with CRLF i.e. \r\n:

Multiple extra headers must be separated with a CRLF (\r\n)

Not so correct form

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 Shape

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 such as: add attachments, multiple recipients, copies or hidden copies .

In addition to the possibility to easily configure sending by SMTP, since some servers do not support the 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;
}
 13
Author: KaduAmaral, 2015-06-12 20:06:44

Using the function mail () it is necessary to pass this information in the header which is the last argument,

Just add this line:

$cabecalho .=   "From: \"{$nome_remetente}\" <{$email_remetente}>\n";
$cabecalho .=   "Bcc: $email_oculto\n";

Bcc : is the blind carbon copy

Cc : carbon copy email

 8
Author: rray, 2014-07-16 20:43:27

According to documentation multiple "additional headers" must be separated by CRLF (\r\n). Only in some cases, and as a last resort, you should do as in your example and separate the headers only with LF (\n). This is because in this way it would not be in accordance with RFC 2822.

So, and according to the information in the function manual, your example would look.:

$cabecalho =    "MIME-Version: 1.0\r\n";
$cabecalho .=   "Content-Type: text/html; charset=iso-8859-1\r\n";
$cabecalho .=   "From: \"{$nome_remetente}\" <{$email_remetente}>\r\n";
$cabecalho .=   "Bcc: $email_oculto\r\n";
 4
Author: bruno, 2015-06-11 16:00:46