I have a PHP script which sends an e-card to multiple recipients in function calls (takes an array of comma-separated email addresses and mail()s to each one individually). However, when looking at the received email, each client can see the other addresses that the email was sent to, making me believe they are all being sent in one email, despite the separate mail() calls. Here is my current code:
<?php
$headers = "From: ".$_POST['email']."\r\n";
$headers .= "Content-type: text/html\r\n";
$array=explode(",", $_POST['sendto']);
for ($i = 0; $i < count($array); ++$i) {
mail(trim($array[$i]), "Happy Holidays!", $body, $headers);
}
?>
How do I fix this so that the recipient can only see their email address in the "to" field?
Thanks!
What you want to use is the BCC field.
https://en.wikipedia.org/wiki/Blind_carbon_copy
Code:
<?php
$_POST['email'] = str_replace(array("\n", "\r"), '', $_POST['email']);
$_POST['sendto'] = str_replace(array("\n", "\r"), '', $_POST['sendto']);
$headers = "From: " . $_POST['email'] . "\r\n"
. "Content-Type: text/html\r\n"
. "BCC: " . $_POST['sendto'] . "\r\n";
mail($_POST['email'], 'Happy Holidays!', $body, $headers);
?>
Send the email to the sender, but BCC the recipients. Also I removed \r and \n chars from the BCC and FROM field otherwise will allow mail header injection attack. Make sure to do the same to $body.
Related
I'm using normal php mail() function to send emails. This works fine on localhost.
But in the online when i try to send a mail with images in the body with <img ../> tag the email won't go. Actually the mail function return true. Even though my inbox won't get any.
WHen I remove the <img> tags and try , emails are receiving to the inbox.
Please help
Thank you
$this->checkAuth();
//headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . ' <' . $from . '>' . "\r\n";
$headers .= 'Reply-To: ' . $from . ' ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($to, $subject, $message, $headers);
$this->_exit();
would be helpful if we could see your code but I'm gonna go out on a limb here and ask you to check your spam folder and make sure the image tags are formatted corectlly and that the email script contains no errors.
also here's a helpful link http://css-tricks.com/sending-nice-html-email-with-php/
Are you only sending a html mail?
Some mail scanners do not allow html only mail and need a plain text mail too, perhaps because you are sending an image it gets reported as spam.
This can block your mail from being send and as far as php is concerned it has been send.
Take a look at: http://kevinjmcmahon.net/articles/22/html-and-plain-text-multipart-email-/
Or the code:
//specify the email address you are sending to, and the email subject
$email = 'email#example.com';
$subject = 'Email Subject';
//create a boundary for the email. This
$boundary = uniqid('np');
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
//Html body
$message .= "
Hello,
This is a text email, the html version.
Regards,
Your Name";
$message .= "\r\n\r\n--" . $boundary . "--";
//invoke the PHP mail function
mail('', $subject, $message, $headers);
This sounds like the emails you send containing the image tags are sent to your spam mailbox.
Are you using some mail provider like gmail? They mostly throw untrusted messages directly into the spam folder. With untrusted I mean accounts which are freshly registered.
It's not something new that mails sent with PHP's native mail() function cause the messages to be detected as spam. It's the way php prepares the message headers.
So, using a php mail library like SwiftMailer is the way to go. It provides you with good customization options and contains functionality that'll make sending mails with attachments a lot easier.
Check it out: http://swiftmailer.org/docs/messages.html#attaching-files
Here's an example of how to use SwiftMailer for sending an email message with an image attachment:
<?php
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password');
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(array('john#doe.com' => 'John Doe'))
// Set the To addresses with an associative array
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html');
// Add the attachment
$message->attach(Swift_Attachment::fromPath('/path/to/image.jpg'));
// Send the message
$result = $mailer->send($message);
Give it a try.
I am tring to sent email to multiplw users in Bcc through PHP mail scriptcode is as follows -
$recipients =array('recpient1#gmail.com','recpient2go#gmail.com');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to = '';
$subject = "E-mail subject";
$body = "E-mail body";
$headers = 'From: info#mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info#mydomain.com' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";
$email =mail($to, $subject, $body, $headers);
if we leave $to black then user in gmail detail shows "to undisclosed recipients" and Bcc not shows how can be show "Bcc me (user who got mail)" i search a lot, but didn't got any proper answer
BCC stands for Blind Carbon Copy and its main purpose is, that you can not see the other recipients of that mail.
If you want to show all email addresses use CC (Carbon Copy) instead.
Be aware, though, that all recipients now can see all other recipients' email addresses, which increases the probabilities having those email addresses end up in spam catalogs.
You can't control how the people at Gmail decide to display the recipients list when reading an email. Each webmail system decides how they want to display the recipients list.
Maybe you can fin another webmail system that decided to display "Sent to me#my-email" instead of "Undisclosed recipients"...but why does it matter to you?
Why do you care about that? I'd say it is a false problem. Since you received the email, it was of course sent to you.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: How to avoid a system generated e-mail going into spam?
How to send 100.000 emails weekly?
I am using the following script to send mail
$to = 'name#test.com' . ', ';
$to .= 'name2#test.com';
$subject = 'Green apple';
$message = 'Enquiry posted by test ';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Green Apple <info#greenappleme.com>' . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo "mail send successfully";
}
else
echo "mail can't send";
When I use this script to some servers, the mail is going to spam. But in some servers, it is going to the Inbox as desired.
How can I prevent email going to spam?
To prevent an email going into spam, don't send with mail from PHP. Send it with SMTP from your server, you can use PHP to connect to SMTP and submit the message. You will then need to set the SPF records on your server, and reverse DNS records with whoever your IP address comes from. If you do these three things then you'll be on the whitelist and all your emails will go into the inbox everywhere, assuming you don't abuse the privilege and get put on a blacklist.
So: send using SMTP, research SPF records and reverse DNS.
You won't be able to do this unless you have a dedicated server with dedicated IP for the domain from which you are sending the email from.
Be sure to set the RETURN PATH as the optional 5th parameter to the mail() function.
if (mail($to, $subject, $message, $headers, '-finfo#greenappleme.com'))
TO send ur majority mail in inbox set below headers and dont use test like keywords in your subject line
$headers = "From: My site<noreply#example.com>\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
I want to make an email forwarder similar to cPanel's, where I have a database of email addresses, and where they should forward to, and I set a catch-all to pipe to my script.
I have completed this script, however, I would like to make the "To:" field in the header show the address it was sent to, rather than who is was being delivered to. For example, the email was sent to user001#mydomain.com, and the script forwards it to me#gmail.com. How can I make PHP send mail to me#gmail.com, but still show user001#mydomain.com in the headers, like cPanel does?
You can use the headers of the mail function:
$to = 'me#gmail.com';
$subject = 'Testing';
$message = 'This is a test';
$headers .= 'To: User001 <user001#mydomain.com>, User002 <user002#mydomain.com>' . "\r\n";
$headers .= 'From: My Email Script <me#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);
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