PHP Mail function, BCC not working - php

I am trying to figure out why the bcc part of this PHP mail function is not working in the code below:
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = "Bcc: <support#mydomain.com>";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
I wouldn't think that there should be any problem specifying a bcc email address that is the same as the From address, but I'm not sure.
When I test this function, the recipient receives the message, but the BCC copy does not come through. Any idea why? Thanks.

Seriously, don't use the mail() function -- you're just letting yourself into a world of hurt.
If you want to do anything beyond absolutely the most basic email, I strongly recommend using a decent mail class, such as phpMailer.
It will make things much easier. No more messing around building the headers yourself, or trying to get the mime types working. Sending to multiple addresses, CC and BCC addresses becomes simple, and adding attachments goes from virtually impossible with mail() to dead simple.
Hope that helps.

try this one in your script you have to change "" to '' and also remove <> then it will be work here i edited your script
function _send_user_email($to, $subject, $message) {
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Customer Service <support#mydomain.com>";
$headers[] = 'Bcc: support#mydomain.com';
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
}
here my mail function example
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: mydemo.com<$your_email>\r\n" .
$headers .= 'Bcc: mydemo#mydemo.com' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
Now If You Want To Use Html In Message
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
For Exampe Message
$message .='<table><tr><td></td></tr></table>';

Try to use Cci like this example:
$headers = array(
'From' => $from,
'To' => $to,
'Cci' => $bcc,
'Subject' => $subject
);

it working fine, i have tested the script found that email drop in Junk box.
try to add name of email holder :
"Bcc: Support <support#mydomain.com>";

Related

warning: mail() expects parameter 4 to be string

I'm trying to use the below code to send mail, but got the below error
Doesn't mail() take in arrays for headers?
Warning: mail() expects parameter 4 to be string, array given in ../email.php on line 16
code :
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'example#gmail.com';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
PHP mail() need string $to, string $subject, string $message, string $headers
if you want to use an array for the headers
mail($to, $subject, $message, implode("\r\n", $headers));
else change your code in
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
why \r\n (from mail() documentation)?
Multiple extra headers should be separated with a CRLF (\r\n) [...]
If messages are not received, try using a LF (\n) only. Some Unix mail
transfer agents (most notably » qmail) replace LF by CRLF
automatically (which leads to doubling CR if CRLF is used). This
should be a last resort, as it does not comply with » RFC 2822.
side note use "\r\n" and not '\r\n'
As the message says the 4th parameter ($additional_headers) should be a string. So you need to join the array elements:
$headers = implode("\r\n", $headers);
mail($to, $subject, $message, $headers);
From the documentation:
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.
You make $headers to be a array, but it has to be a string! So just change these lines:
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
to this and concatenate the strings together:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";

php mail() to multiple recipients - dont share email adresses

im sending a batch email to multiple recipients using:
mail(implode(',', $emails), $subject, $content, $headers);
however, each person can see the list of who the email was sent to. I want to keep this private, and also so the email appears more personal.
is there a way to do this without sending a mail() to each email, as I'm guessing this will take a long time to run?
You are looking for a simple BCC address. Everyone in the same mail but not able to see each others email address.
Look here: http://php.net/manual/en/function.mail.php and find the BCC.
this is the piece you need:
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender#domain.com>";
$headers[] = "Bcc: JJ Chong <bcc#domain2.com>";
$headers[] = "Reply-To: Recipient Name <receiver#domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $email, implode("\r\n", $headers));
add a bcc header to to your message
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Bcc: email' . "\r\n";
mail($to, $subject, $message, $headers);

php mail header allow ascii characters

On sending a mail I've set my header to:
$headers = 'From: Tómas<tomas#email.com>'. "\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
for mail($to, $subject, $message, $headers);
The Tómas is working in the content correctly but not in the from section of the email. It shows up like Tómas. Any idea how to modify the header to make this work?
Many thanks.
Tó = Tó
change the following line
$headers = 'From: Tómas<tomas#email.com>'. "\r\n";
with
$headers = 'From: Tomas<tomas#email.com>' . "\r\n";

PHP mail() function not sending email

I am attempting to send an email using the mail() PHP function. I had it working until I attempted to give it a subject of "User registration", then the mail is not sent!
Heres the code (have simplified it greatly)
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: noreply#example.com' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <admin#example.com>';
mail($to, 'User Registration', $message, $headers);
I also attempted to use a variable containing the string of text but that didnt work.
Why is it not sending the mail when I add the subject exception?
Thanks
EDIT: updated code thats still not working
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin#example.com>';
mail($to, 'User Registration', $message, $headers);
On your 4th line you're using ' that handles everything inside of it as a string so change
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
To:
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
and as mentioned in the comments change chareset to charset
Edit:
if your sending a txt/html mail you have according to documentation to set mime in the headers too so try this
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin#example.com>' . "\r\n";
mail($to, 'User Registration', $message, $headers);
If it still doesn't work you could try to debugg your code, simply add
error_reporting(E_ALL);
ini_set('display_errors', '1');
on top of the page, and take it from there, and if you still can't solve it by yourself post it here and I'll do my best to help ya out.
I'm using this code on most of my projects:
$subject = 'subject';
$message = 'message';
$to = 'user#gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';
$mail = 'no-reply#'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid = md5(uniqid(time()));
$headers = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'#'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';
mail($to, $subject, $message, $headers);
I would suggest to use PHP_EOL instead of \r\n or \n as the line break would then be determined by your environment...
$headers = 'MIME-Version: 1.0' . PHP_EOL;
etc.. hoping this might finally solve your problem!

php mail function - how can changing From: in header cause email not to be sent?

The one email From:support#lead.com works fine. But when changed to john.doe#lead.com it doesn't work? See below.
Both are valid email addresses (fictitious for this example) in the domain from which they are sent.
I have the question into Netfirms support too. I expect to move to phpmailer or API to a ESP (e.g., mailchimp) account soon, but this is just bugging me that the little change breaks the email function.
Code:
Works:
$headers = "Mime-Version: 1.0\n";
$headers .= "Content-Type: text/html;charset=UTF-8\n";
$headers .= 'From: support#lead.com' . "\r\n";
$headers .= 'Bcc: bcc#lead.com' . "\r\n";
// $headers .= 'Return-Path: bcc#lead.com' . "\r\n";
mb_internal_encoding("UTF-8");
if (!mail($to, $subject, $body, $headers, "-f bcc#mylead.com")) echo ("Message delivery failed");
Doesn't Work: (only changed support to john.doe):
$headers = "Mime-Version: 1.0\n";
$headers .= "Content-Type: text/html;charset=UTF-8\n";
$headers .= 'From: john.doe#lead.com' . "\r\n";
// $headers .= 'Return-Path: bcc#lead.com' . "\r\n";
mb_internal_encoding("UTF-8");
if (!mail($to, $subject, $body, $headers, "-f bcc#lead.com")) echo ("Message delivery failed");
Don't know how this really should matter, but you're mixing \n and \r\n in the headers, which may confuse your mail server... Would you mind to try this out?

Categories