PHP send mail to multiple recipients without sharing email addresses - php

I am trying to send email to multiple recipients using PHP. My code is like this
<?php
foreach($subscribers as $subscriber){
$email[] = $subscriber['email'];
}
$emails = implode(',', $email);
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($emails, $subject, $email_text, $headers);
?>
I am able to send emails correctly. But it shows others email addresses in there email box and I don't want to share email addresses with each other. Please help how can I do this. I tried to send email inside the for each loop like this
<?php
foreach($subscribers as $subscriber){
$email = $subscriber['email'];
$email_from = "email#example.com";
$subject = "My Subject";
$full_name = 'Example Sender';
$from_mail = $full_name.'<'.$email_from.'>';
$email_text = file_get_contents('email.html');
$headers .= 'From: ' . $from_mail . "\r\n".
"MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
mail($email, $subject, $email_text, $headers);
}
?>
But it sends email to only the first email address inside loop. Please help.

You can add an empty string in the $email field and use bcc headers to add the mail addresses. bcc makes sure the receiver can't see the other email addresses.
$headers .= "Bcc:email#example.com" . PHP_EOL;

Related

Email reply to multiple email addresses not working

I want that email reply in php should be received at multiple email address. But it is not working for me. I have tried different ways but none of these working for me.
I am trying this header but it is only replying to the last email address.
$to = "recipientemail#address.com";
$subject = "my email subject";
$message = "my email message";
$headers = array();
$headers []= "MIME-Version: 1.0\r\n";
$headers []= "Content-Type: text/html; charset=UTF-8\r\n";
$headers []= "Reply-To: <" . $EmailAddress1 . "> <" . $EmailAddressss2 . ">" . "\r\n" ;
wp_mail($to, $subject, $message, $headers);
I have tried without braces as well but this one is also not working
$headers []= "Reply-To: 1st#address.com,2nd#address.com\r\n";
I do not have much idea about this so little guidance about this will be much appreciated. Thank you!

how to Send Email details notification using php?

How can I send email notification to my users who gave registration details in my form using php?
I have code which runs perfectly to get emails to me, but now I also want to same details to my users.
I am trying to use "$from" into my array at "$to" but getting no email.
my mail.php
<?php
$subject = "Email Notification";
$message = "Thank you for your email";
$message = "Your Registering details are as follows:";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = $_POST['email'];
$to = array('my_address#example.com', 'my_address2#example.com', $from);
$lp = "notification#example.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$lp .'' . "\r\n" .
'Reply-To: '.$lp.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
It's pretty common that on shared hosting you need to send an email from an "existing email", at least match the domain. Maybe that's why the emails are not being sent?
So for example, if your domain is "www.my-awesome-domain.com", you can't send email headers
"from: example#example.com"
Instead, make sure to send emails from your domain, and ideally an existing email box, for example:
"from: office#my-awesome-domain.com"
Hope it helps! :)

How do I include sender's name and not just the email address while emailing message from user input form

I am trying to take inputs from a form on my website using a simple PHP script as given below:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
This worked perfectly. Well, almost. The email I receive does not include the sender's name. I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.)
So I made a few changes after looking up some threads like this:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;
if(mail($toemail, 'Subject', $message, $header)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
Email response is still same. What do I need to change ?
Try this format, note the spaces and the \r\n's, change your variables accordingly:
$email_headers = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <sender_email#yourdomain.com>' . "\r\n";
$email_headers .='Reply-To: reply_to_email#yourdomain.com' . "\r\n";
mail($recipient_address, $email_subject, $email_body, $email_headers);
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Quoted from http://php.net/manual/en/function.mail.php

Add recieving email adress in contact form

I'm working with contact form
This is part where email is being sending
$emailTo = 'myemail#gmail.com';
$subject = 'Wiadomość ze strony Mud';
$sendCopy = trim($_POST['sendCopy']);
$body = "Email: $email \n\nKind of frame: $frame \n\nColor suggestion: $color \n\nInfo about project: $comments";
$headers = 'From: ' . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
Form works good everything is fine, except when I get mail and I want IN MY MAILBOX reply email it shows adress of my webserver
It looks like $headers dont work properly, If I'm right this part of code is responsible for reply adres email "'Reply-To: ' . $email;"
(IM NOT GOOD IN PHP)
And my second optional question is how to make stylish email.
I mean for example in my email when it comes to mailbox it shows
Email: dsadas#dsdas.pl
Kind of frame: dasdsa
Color suggestion: dasdsa
Info about project: dasdsa
How can I do it bold, color etc ?
THANK YOU FOR YOUR TIME, CHEERS
Here you go for headers that will allow HTML for email and fix your reply to email:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $emailTo <$emailTo>" . "\r\n";
Remember that there is limited support for email when it comes to CSS, you can see things that can't be used here: http://www.campaignmonitor.com/css/

get list e-mail list from database and send mail to all from php script without using any api

$sql = "select emailid from table where category = 1";
while($row=mysql_fetch_array($sql))
{
$email=$row['emailid'];
$to = $email;
$subject = "E-mail subject";
$body = "E-mail body";
$headers = 'From: info#mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info#mydomain.com' . "\r\n";
mail($to, $subject, $body, $headers);
}
Suppose the above code will fetch 100 e-mail ids from database and send the mail one by one to each e-mail id. but what i want to do is.... fetch all e-mail ids in an array and send them as 'BCC' to each e-mail at once. one more thing i want to customize the e-mail body content for each id....
Any ideas?
What you want to do is impossible. If you send them at once via multiple addresses in To/CC/BCC, everyone gets exactly the same email. So you cannot customize the bodies.
If sending the same email to everyone is fine, try this:
$sql = mysql_query("select emailid from table where category = 1");
$recipients = array();
while($row = mysql_fetch_array($sql)) {
$recipients[] = $row['emailid'];
}
$to = 'info#mydomain.com';
$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";
mail($to, $subject, $body, $headers);

Categories