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

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);

Related

How do I display the user's name before the their email address when sending PHP mail?

For example in the Gmail application, when clicking "show details" there would be
"To: whatever#gmail.com". What I want is to show a full name before that,
for example:"My Name whatever#gmail.com".
I think I would have to add something within $to = "whatever#gmail.com". Any idea on how to achieve this? Thanks.
Copy/paste from the docs:
$to_name = "Mary";
$to_address = "mary#example.com";
// Additional headers
$headers[] = 'To: '.$to_name.' <'.$to_address.'>, Kelly <kelly#example.com>'; // <-- this is what you want?
$headers[] = 'From: Birthday Reminder <birthday#example.com>';
$headers[] = 'Cc: birthdayarchive#example.com';
$headers[] = 'Bcc: birthdaycheck#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
If you have the IMAP extension you can use imap_rfc822_write_address().
echo imap_rfc822_write_address("hartmut", "example.com", "Hartmut Holzgraefe");
Or you can just wing it and hope everyone uses "safe" email addresses.
echo "$name <$email>";
The actual header would be:
To: NameHere <email#example.com>\r\n

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";

Why I send a mail with PHP, but receiver get a mail with attachment named "mail.html"?

$mailto = 'example#mail.com';
$subject = 'foo';
$message = 'bar';
mail($mailto, $subject, $message);
I open with the mail with OUTLOOK 2010, it display normally. But the mail has an attachment named "mail.html" that display "bar" when I open it in foxmail.
What is difference between these mail client? So this problem isn't seem a programming problem.
Did you use any headers?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
Don't use foxmail so no clue if it works...but if you didn't send headers it's worth a try.

PHP Mail function, BCC not working

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>";

Multiple recipient mail php not working for second onwards addresses

I have an HTML email needed to be sent to more than one person:
$mem = "abc#def.com, qwr#rty.com";
$subject = 'Invitation to Party';
$headers = "From: info#example.com\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "SNIPPED";
mail($mem, $subject, $message, $headers);
It is sending to the first email which is abc#def.com, but it's not sending mail for the second recipient which is qwr#rty.com and the rest.
The email addresses are examples.
Is there any work-around for this besides using library?
you may send the second id as a bcc,
$headers .= "Bcc: ".$bcc."\r\n";

Categories