PHP Email sending BCC - php

I know there are a few similar questions to this but I just can't get it working.
Ok, I have a list of emails grabbed from my database in a variable called $emailList.
I can get my code to send an email from a form if I put the variable in the $to section but
I cannot get it to work with bcc. I've even added an email to the $to incase it was that but it doesn't make a difference.
Here is my code.
$to = "name#mydomain.com";
$subject .= "".$emailSubject."";
$headers .= 'Bcc: $emailList';
$headers = "From: no-reply#thepartyfinder.co.uk\r\n" . "X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';
if (mail($to, $subject, $message, $headers)) {
$sent = "Your email was sent!";
} else {
$sent = ("Error sending email.");
}
I've tried both codes:
$headers .= 'Bcc: $emailList';
and
$headers .= 'Bcc: '.$emailList.';
It's not that the emails aren't separated because they are. I know they are because it works if I put $emailList in the $to section.
I Should add, ignore the $message bits and the HTML stuff. I've not provided all of that so that is why it's missing from this code.

You have $headers .= '...'; followed by $headers = '...';; the second line is overwriting the first.
Just put the $headers .= "Bcc: $emailList\r\n"; say after the Content-type line and it should be fine.
On a side note, the To is generally required; mail servers might mark your message as spam otherwise.
$headers = "From: no-reply#thepartyfinder.co.uk\r\n" .
"X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: $emailList\r\n";

You were setting BCC but then overwriting the variable with the FROM
$to = "name#mydomain.com";
$subject .= "".$emailSubject."";
$headers .= "Bcc: ".$emailList."\r\n";
$headers .= "From: no-reply#thepartyfinder.co.uk\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';
if (mail($to, $subject, $message, $headers)) {
$sent = "Your email was sent!";
} else {
$sent = ("Error sending email.");
}

Related

How to merge e-mail headers data into e-mail html message body in PHP

I'm looking for a solution to put the e-mail's header like address, subject, date etc... Into the e-mail's message body. Like it works when forwarding an e-mail from any e-mail client.
Is there a good practice for it in PHP? I couldn't find it in the manual.
I'm using php with laravel.
$to = 'me#mydomain.com';
$subject = 'Subject';
$headers = "From: me# mydomain.com\r\n";
$headers .= "BCC: someonelese# mydomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= "<html><body>…Email Message 1…</body></html>";
mail($to, $subject, $message, $headers);
}
if(count($errors) == 0) {
$to = ($_POST['email']);
$subject = 'Subject';
$url = 'mydomain.caom';
$headers2 = "From: me# mydomain.com\r\n";
$headers2 .= "BCC: someonelese# mydomain.com\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>…Email Message 2…</body></html>";
mail($to, $subject, $message, $headers2);
echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
}
Can I just concatenate the headers into the body?
Yes you can!
You can do something like this
$to = 'me#mydomain.com';
$subject = 'Subject';
$headers = "From: me# mydomain.com\r\n";
$headers .= "BCC: someonelese# mydomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message_headers = str_replace("\r\n", "<br>", $headers);
$message = "<html><body>…Email Message 1…{$message_headers}</body></html>";
mail($to, $subject, $message, $headers);
The above will replace the new lines (\r\n) in $headers with the HTML <br> tag and the formatted headers will be added to the $message just before the closing <body> tag.
Also notice that I have removed the . from the $message .= "..."; and replaced it with $message = "...";
You dont need the . unless you are appending the $message variable.

How to create email content type to multipart/mixed in php

I have a website contact form that sends as an email to my iPhone. The problem is I am getting the following message:
"This message cannot be displayed because of the way it is formatted. Ask the sender to send it again using a different format or email program. multipart/mixed"
Is this has something to do with email content-type and how to fix it in my PHP code.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
$sentMail = mail($recipient_email, $subject, $body, $headers);
You specify the Content-Type as a header string.
Assuming you are appending your headers with the standard dot notation, you would use:
$headers .= "Content-Type: multipart/mixed";
And later this would become the fourth parameter of your mail() function:
mail($address, $subject, $message, $headers);
Here's a basic example:
$address = 'sample#sample.com';
$subject = 'Subject';
$message = 'Message';
$headers = "From: My Name<something#something.com>\n";
$headers .= "Reply-To: something#something.com \n";
$headers .= "Content-Type: multipart/mixed";
mail($address, $subject, $message, $headers);

Send BCC with PHP mail() [duplicate]

I know there are a few similar questions to this but I just can't get it working.
Ok, I have a list of emails grabbed from my database in a variable called $emailList.
I can get my code to send an email from a form if I put the variable in the $to section but
I cannot get it to work with bcc. I've even added an email to the $to incase it was that but it doesn't make a difference.
Here is my code.
$to = "name#mydomain.com";
$subject .= "".$emailSubject."";
$headers .= 'Bcc: $emailList';
$headers = "From: no-reply#thepartyfinder.co.uk\r\n" . "X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';
if (mail($to, $subject, $message, $headers)) {
$sent = "Your email was sent!";
} else {
$sent = ("Error sending email.");
}
I've tried both codes:
$headers .= 'Bcc: $emailList';
and
$headers .= 'Bcc: '.$emailList.';
It's not that the emails aren't separated because they are. I know they are because it works if I put $emailList in the $to section.
I Should add, ignore the $message bits and the HTML stuff. I've not provided all of that so that is why it's missing from this code.
You have $headers .= '...'; followed by $headers = '...';; the second line is overwriting the first.
Just put the $headers .= "Bcc: $emailList\r\n"; say after the Content-type line and it should be fine.
On a side note, the To is generally required; mail servers might mark your message as spam otherwise.
$headers = "From: no-reply#thepartyfinder.co.uk\r\n" .
"X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: $emailList\r\n";
You were setting BCC but then overwriting the variable with the FROM
$to = "name#mydomain.com";
$subject .= "".$emailSubject."";
$headers .= "Bcc: ".$emailList."\r\n";
$headers .= "From: no-reply#thepartyfinder.co.uk\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';
if (mail($to, $subject, $message, $headers)) {
$sent = "Your email was sent!";
} else {
$sent = ("Error sending email.");
}

Undisclose recipients php without any extensions only mail() function

I am trying to not disclose the recipients when I send an email but I am failing on this:
$email = implode('; ', $email); // array where I have the emails
$to = $email; // webmaster#domain.com; webmaster#anotherdomain.com
$subject = 'Subject';
$headers = "From: noreply#domainfromwhereisendemail.com\r\n" . "X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: Undisclosed Recipients <noreply#domainfromwhereisendemail.com>\r\n";
//$headers .= "Cc: $to\r\n";
$message =
<<<START
This is the message
START;
mail($to, $subject, $message, $headers);
I tried adding Cc or Bcc, but not working, it is adding besides the emails Undisclosed recipients. I am trying to do this without any other extensions, I checked a lot of questions from stackoverflow here but did not accomplish this. Emails are still being shown to each.
Nope you can't stop that from nondisclosure, unless you send the emails separately using a loop.
$email = implode('; ', $email); // <---- Don't do this.
The loop up way..
foreach($email as $mail) #<---- Use a foreach and loop through
{
$to = $mail;
$subject = 'Subject';
$headers = "From: noreply#domainfromwhereisendemail.com\r\n" . "X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message =
<<<START
This is the message
START;
mail($to, $subject, $message, $headers);
}
Here is the ôkio solution with a single call to mail():
$subject = 'Subject';
$headers = "X-Mailer: php\r\n";
# $headers .= "MIME-Version: 1.0\r\n"; # do you really need that?
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
foreach($email as $mail){
$headers .= "Bcc: ".$mail."\r\n";
}
$message =
<<<START
This is the message
START;
mail('non#existing.email', $subject, $message, $headers);

PHP Mail Sending BCC not wordking

I am using PHP mail and trying to send BCC, but for some reason since I've added the lines with //ADDED NEW on it , it's just now sending any emails at all.
Here is the full code:
$to = "me#gmail.com";
$bcc = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $emailList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $bcc, $subject, $message, $headers); // $bcc ADDED NEW
Why is this not sending?
Your problem,
Set $bcc but in $headers does not using it
Putting invalid argument into mail function.
Try this
$to = "me#gmail.com";
$bccList = $row['recipients']; //ADDED NEW
$subject = $row['subject'];
$message = $row['text_body'];
$headers = "From: " . strip_tags('me#gmail.com') . "\r\n";
$headers .= "Reply-To: ". strip_tags('me#gmail.com') . "\r\n";
$headers .= "Bcc: $bccList\r\n"; //ADDED NEW
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers); // $bcc ADDED NEW
There's no $bcc argument to the mail() function. It should be:
mail($to, $subject, $message, $headers);
The blind recipients will be retrieved from the Bcc header in $headers.

Categories