how to send many emails in one time by php - php

i want know
how i can send one mail to 200 user
and i want when the email sendt
it appeare send to:user#mail.com
from:my mail
because i think i see many email like that
to:email1#exaple.com;email2#exaple.com;emial3#exaple.com;
ithink this from bbc
i mean i want every user see sent to this email only

You can use the native mail() function, separate the addresses with a comma, and put them all in the blind carbon copy list with additional headers.
$to = "jon#abc.com,sal#example.com";
$subject = "Mini-mass Emailer";
$message = "Hello World";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
$headers .= 'Bcc: {$to}' . "\r\n";
mail($to, $subject, $message, $headers);
Or you could iterate over the collection of email addresses and send each message individually:
$emails = array("foo#bar.com","fizz#buzz.com");
foreach ($emails as $email) {
$to = $email;
$subject = "My Subject";
$message = "Hello World";
mail($to, $subject, $message);
}

Send each mail separately to each user. You can write a helper utility function to do this iteratively.

if use mail function
use bcc in header
like this :
$headers .= 'Bcc: 1#example.com,2#example.com.............' . "\r\n";
u can use a class like phpmailer
and use AddBCC() function

Related

sending only BCC mail in php

I am pulling email addresses from my mailing list in a txt file. with the following:
clearstatcache();
$file = file("test.txt");
for ($i = 0; $i < 20; $i++) {
$emails .= $file[$i];
}
As you can see I've stored them in $emails. and if I echo $emails, I get the emails listed:
info#example.com, test#mydomain.com, admin#domain.com, etc.
now to sending the BCC:
// recipient
$to = '';
// subject
$subject = 'The subject is here';
// message
$message = 'The body of the email is here';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: John Doe <info#example.com>' . "\r\n";
$headers .= 'Bcc: '.$emails . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
The mail doesn't get sent to the list, only to info#example.com - So this doesn't work and something unexpected happens - for some strange reason the 20 emails from the for loop are listed at the top of the email body when received by info#example.com
When I try manual input it works perfectly. so the code below works, but manual input is counter productive to what I am trying to achieve.
$test = "info#example.com, test#mydomain.com, admin#domain.com,";
// recipient
$to = '';
// subject
$subject = 'The subject is here';
// message
$message = 'The body of the email is here';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: John Doe <info#example.com>' . "\r\n";
$headers .= 'Bcc: '.$test . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
So it appears that the issue is in the variable, but I can't figure out why it doesn't work because $emails echoes out all the email addresses fine.
White space is automatically added to the end of line. This change in the for loop fixes the issue.
$emails .= trim($file[$i]);
You must close the loop after:
mail($to, $subject, $message, $headers);
}
and
$headers .= 'Bcc: '.$emails . "\r\n";
is
$headers .= 'Bcc: '.$file[$i] . "\r\n";
So the loop will run the entire program 20 times. Do not place recipient in $ to because otherwise will send 20 times as well.
Tested and works great.

Personalised 'To' field when sending large email list using PHP mail and multiple addresses in the Bcc

Im trying to remember a trick i was taught a while back but can not.
Basically, im using PHP mail() in this fashion:
$to = "emailAddress1#domain.com";
$subject = "Welcome to BuildSanctuary";
$messageContent = "Thankyou for registering Bla bla bla";
$message = 'SOME HTML EMAIL STUFF including the $messageContent var';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: Accounts<accounts#domain.com>' . "\r\n";
$headers .= 'Bcc: emailAddress1#domain.com,emailAddress2#domain.com,emailAddress3#domain.com,emailAddress4#domain.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
I got told a while back that there was a way that you could put your own email in the to field, but when the users receive the emails it looks like they were personally sent the email and the to field shows just their email.
Is this possible? Cant think how.
Thanks.

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.

Sending a Confirmation Email in PHP

I am quite new to PHP and am experimenting with an online ordering website in php. I have come across a problem when trying to send a confirmation email using a 'cc'.
Every time an order is processed, the order always sends to the 'CC' address specified as the '$from' and the sender (the from) is what is specified in the '$to' section. In the email confirmation received, it only displays the from section and the 'to' and 'cc' are empty as demonstrated below:
From: **$_SESSION['od_email']**
To: *This space is empty*
CC: orders#business.co.uk
Can anyone help to point out where I am going wrong? I have attached the code below.
//THIS IS CODE FOR THE EMAIL WHICH IS AUTOMATICALLY SENT TO THE CUSTOMER AND THE BUSINESS WHEN AN ORDER IS SUCCESSFULLY COMPLETED
//define the receiver of the email
$to = $_SESSION['od_email'];
//define the subject of the email
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
//define the message to be sent. Each line should be separated with \n
$message = 'test';
//Who the message is from
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From:" . $from;
//bcc header going to the to go to the business as confirmation
$headers = "cc:" . $cc;
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
What I need for it to show is:
From: **orders#business.co.uk**
To: **$_SESSION['od_email'];**
CC: **orders#business.co.uk**
Thanks in advance for any help given
When you're going to set the CC, you're overwriting what was in $headers. Try:
$headers = "From:" . $from . "\r\n";
$headers.= "Cc:" . $cc;
And if that doesn't help, there's always PHP's documentation on mail.
I would go all out with your headers and make sure you set everything you need to,
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
also, I believe mail() needs a valid email address, I do not know how you have the email set before $_SESSION['od_email'] but if you are running a test and literally expecting $_SESSION['od_email'] to come with it that wont work, it will see it as a not valid email and return false, thus returning nothing. To further diagnose that problem tho we would need to know how $_SESSION['od_email'] is setup
EDIT
Okay I am pretty sure the issue is caused how you are setting up $_SESSION['od_email]' based on your comment below.
Note: I am assuming your database is $db, but if not you need to change $db to the prober name
Let's try this and see how it works
// Get Data from the Database
$query = "SELECT `od_email` FROM `tbl_order`";
$result = $db->query($query);
while ($data = $db->fetch_array($result)) {
// Set Up the SESSION email
$_SESSION['od_email'] = $data['od_email'];
// Prepare the Email
$to = $_SESSION['od_email'];
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
$message = 'test';
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
Couldn't he just add an #mail ? such as...
$mail_sent = #mail( $od_email, $subject, $message, $headers );
or
mail( $od_email, $subject, $message, $headers );
Replacing any variables.
Always worked for me.

send mail by bbc not working

hay
i used this code
$to = "mial#live.com,mail#yahoo.com";
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
$headers .= 'Bcc: {$to}' . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo 'ok';
}
but see what is happend
every user see the full list of the users
alt text http://img694.imageshack.us/img694/1289/21811933.gif
Your call to mail is passing the $to as the to parameter meaning those emails will be be in the to header try passing an empty string instead. You are passing the info into the bcc header so the email should still get to them that way.
That is because you have put all the users in the "to" line. You are also passing them into the "bcc" line too so just doing this may help you but as far as I know you need at least one address in the to line (although this may not be the case). It'll look pretty strange for each person doing it that way though.
The best way to avoid these issues would be to send the email multiple times, once to each user. To modify your code example to do this, I'd do something like the following:
$toAddresses = array("mial#live.com", "mail#yahoo.com");
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
foreach ($toAddresses as $to) {
if(mail($to, $subject, $message, $headers)){
echo "OK - sent message to {$to}";
}
}
The easiest way is to take this Mail-Class of phpguru.org:
http://www.phpguru.org/static/htmlMimeMail5
There you can specify with setBcc() the addresses which should be "blind", it's pretty easy and works well. I use this class in every project.
Best Regards.

Categories