I read that
All custom headers (like From, Cc, Bcc and Date) are supported
from PHP mail() reference, but how do I actually implement this in my code? i.e. I don't want any recipient to see the other e-mails... ever.
Something like:
<?php
$to = "someguy#somesite.com, another#abc.com, someother#def.com";
$subject = "test message";
$msg = "this is a test";
$headers = "Bcc"; // <-- this?
if (mail($to, $subject, $msg, $headers))
echo "message sent";
else
echo "uh oh";
?>
Look at the examples on the page.
Here's one...
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
So you just add comma separated email addresses after Bcc: in the header.
Here's one with a loop that will add addresses from the array $recip to BCC:
$headers.="Bcc: ";
while($count < $count_recip){
$headers.=$recip[$count].", ";
$count ++;
}
Add the address to the envelope (i.e. the list of recipients), but not the message (i.e. the headers).
Related
I brought a shared sever, and got a little storage space. When i run my web site, the mail function works perfectly. Mail has been sent with subject and message to all receivers. But the mail which is sent mentioned "From : ". Headers what i included are not working.
I want to send with my headers Eg:Form: mymail#mysite.com . How can i do it?
<?php
$output="";
if(isset($_POST['submit']) && !empty($_POST['message']))
{
$sub=$_POST['subject'];
$msg=$_POST['message'];
$sql = "select email from login where status='client'";
$query = mysqli_query($con,$sql);
$to=array();
while($row = mysqli_fetch_array($query))
{
$to[] = $row['email'];
$parts = implode(',',$to);
$headers = 'From: mymail#mysiet.com;';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$subject = $sub;
$content = $msg;
}
if(mail($parts,$subject,$content,$headers))
{
$output="mail sent";
}
else
{
$output="Error in connection, Mail could not be sent. Please try again";
}
}
?>
You need to manually specify the headers, this is the function I use and it delivers to pretty much every provider beautifully. The headers you want are From: and Reply-To: and I also advise the X-Mailer follows what is seen here, I found it years ago via SO as well.
<?php
$to = "user#anotherdomain.com";
$headers = 'From: My Name <email#domain.com.au>' . "\r\n" .
'Reply-To: My Name <email2#domain.com.au>' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-type: text/html; charset=iso-8859-1';
$subject = "My subject line here";
$message = 'Message HTML and Content Here.';
if(mail($to, $subject, $message, $headers)){
// Success Here
}else{
// Failed Here
}
?>
I am not 100% certain it will work for you as I am on a dedicated machine, having said that - I do use it on multiple domains and have never messed with my Apache or php configuration in relation to mail settings.
Read More about the Mail function in PHP: http://php.net/manual/en/function.mail.php
You can try below code, just ripped from php.net
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
Off topic but good to use PHPMailer.
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.
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.
I have set up a mail form to send emails from my webpage, and I would like to be able to set up images in these emails. This is the code I currently have:
$to = "test#test.com";
$subject = "Emergency details";
$body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
When I send this email, the outputs looks like this:
Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/test.jpg"/>
and actually displays the code rather than the picture. Is it possible to make this display the picture instead?
Thanks for any help
That's because you're actually sending a text mail and not an HTML mail. You have to set the proper headers.
Have a look at the mail() function manual : http://php.net/manual/en/function.mail.php
Specifically : Example #4 Sending HTML email
// 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";
you are sending this email as plain text. You should specify that it should be interpreted as an html mail by using mail()'s fourth argument (headers).
Example can be found in the documentation.
The snippet:
// 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: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
You are sending a simple mail and due to this it is interpreted as text only. What you want to do is to send a mail containing html instead of simple text. This require that you include headers to the mail explaining the content of the mail.
Try this:
$headers .= "--$boundary\r\n
Content-Type: text/html; charset=ISO_8859-1\r\n
Content-Transfer_Encoding: 7bit\r\n\r\n";
$to = "test#test.com";
$subject = "Emergency details";
$body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
(example snapped from http://www.daniweb.com/web-development/php/threads/2959)
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.