Ik heb een php code die ik al langer gebruik om een pdf als attachment te versturen per mail. Dit werkt prima.
Nu probeer ik een bcc toe te voegen, en toch werkt het niet. Weet iemand wat ik hier mis?

$mail_from
$mail_to

Zijn voorzien van een e-mailadres.


$mailMessage = "<strong>Bijgaand het PDF-document</strong>";

$mailAttach = "pdf/".$kenmerk.".pdf";

// GENERATE AFSCHEIDING TUSSEN BERICHT EN ATTACHMENT
$mailBoundary = md5(time());
$mailHead = 'From: '.$email_from."\r\n";


// toegevoegd bcc check, dan $mailHead complementeren
if ($kopie=="1") {
	 $mailHead .= 'BCC: '.$email_from."\r\n";
}
// einde toevoeging en check

$mailHead .=  'X-Mailer: PHP/' . phpversion(); 

$mailHead .= implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-Type: multipart/mixed; boundary=\"$mailBoundary\""
]);

// TUSSENDEEL
$mailBody = implode("\r\n", [
  "--$mailBoundary",
  "Content-type: text/html; charset=utf-8",
  "",
  $mailMessage
]);	

// DECODING ATTACHMENT
$mailBody .= implode("\r\n", [
  "",
  "--$mailBoundary",
  "Content-Type: application/octet-stream; name=\"". basename($mailAttach) . "\"",
  "Content-Transfer-Encoding: base64",
  "Content-Disposition: attachment",
  "",
  chunk_split(base64_encode(file_get_contents($mailAttach))),
  "--$mailBoundary--"
]);

// STUUR DE MAIL NU MET BIJLAGE-ATTACHMENT
echo mail($mailTo, $mailSubject, $mailBody, $mailHead);
Even ter leering ende vermaeck een voorbeeld hoe dit met PHPMAiler gaat:

<?php
$mail = new PHPMailer(true);
try {
$mail->setFrom('[email protected]', 'mijn website');
$mail->addAddress('[email protected]', 'Joe krijgt de mail');
$mail->addAddress('[email protected]', 'Jane ook');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$mail->addAttachment($mailAttach); // verwijzing naar de file op jouw server.

$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
// mail is verstuurd: geef een succesmelding of redirect de gebruiker
// mail kan altijd nog ten prooi vallen aan spamfilters maar is in elk geval op weg gestuurd
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

[size=xsmall]Toevoeging op 25/08/2022 09:28:50:[/size]

Ik vind het een groot voordeel dat je niet zelf keer op keer een script moet maken met de losse headers en de opdeling met mailboundry's en de content-type headers bij attachments.
@Ivo P, dank je wel. Ik heb dit toegepast bij een nieuwere site. Maar soms moet je met oude code rommelen. Fijn dat je hielp.

Reageren