i have a string like:
$email = 'xyz#email.com,abc#email.Com,and others#email.com';
AND
$msg = 'SEND these to each email above thanks';
in my achievement i want to be able to send text in the $msg as an email message to each email in $email example:
$to = 'xyz#email.com';
$msg = 'SEND these to each email above thanks';
$to = 'abc#email.com';
$msg = 'SEND these to each email above thanks';
$to = 'others#email.com';
$msg = 'SEND these to each email above thanks';
foreach($email as $email)
{
$to = $email["to"];
$subject = 'the subject';
$message = '$msg';
$headers = 'is this really? ';
mail($to, $subject, $message, $headers);
}
big thanks for your impact in my soluction
Try splitting the emails string and for each email execute the code that sends the mail
<?php
$emails = 'email1#example.com,email2#example.com';
$emailsSplitted = explode(',', $emails);
foreach ($emailsSplitted as $email) {
// ...
// use here the variable $email to send the message
}
Related
I have the following code saved as send.php:
<?php
$to_email = "person#gmail.com";
$subject = $_POST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
The email sends if I navigate to https://myWebsite.com/Stuff/send.php
and make $subject = "something"
But if I set $subject to be a POST variable. It does not send the subject when I write this URL in the browser (as in, the email still sends but the subject is now blank):
https://myWebsite.com/Stuff/send.php?subject=notworking
To get both $_POST and $_GET you can use $_REQUEST which will work for both.
In your code replace
$subject = $_GET['subject'];
With
$subject = $_REQUEST['subject'];
If someone direct hit the URL in browser without paramter subject you can prevent them using
if(!empty($_REQUEST['subject']))
{
$to_email = "person#gmail.com";
$subject = $_REQUEST['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
}
If you want to extract the values from the URL parameters then you need to use the $_GET
global variable to get the data like this,
<?php
$to_email = "person#gmail.com";
$subject = $_GET['subject'];
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>
Try it.
<?php
$to_email = "person#gmail.com";
$subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";
$body = "Hi, This is test email send by PHP Script";
$headers = "From: Me#myWebsite.com";
if ( mail($to_email, $subject, $body, $headers))
echo("Email successfully sent to $to_email...");
else
echo("Email sending failed...");
?>
I have this code:
<?php
$emails = 'email1#example.com,email2#example.com';
$emailsSplitted = explode(',', $emails);
foreach ($emailsSplitted as $email) {
$to ='$email';
$message = 'send this to all email above';
$subject = 'the subject';
$headers = 'From: multiplememail#gm.Com';
mail($to, $subject,$message, $headers);
}
$str = "abc#email.com[thanks]wap#email.com[i want also]data#email.com[multiple msg]";
$regExpresion = "/(.*?)\[(.*?)\]/";
preg_match_all($regExpresion, $str, $aMatches, PREG_SET_ORDER, 0);
foreach ($aMatches as $aMail){
$to = $aMail[1];
$message = $aMail[2];
$subject = 'the subject';
$headers = 'From: multiple#gm.Com';
mail($to, $subject,$message, $headers);
}
$to = 'abc#email.com';
$message = 'single message';
$subject = 'the subject';
$headers = 'From: singleemail#gm.Com';
mail($to, $subject, $message, $headers);
which send email line by line just as is it in my code above but am sorry, just want to know if i can use just one mail function line to deliver this than doing it for each string record. thanks
Yes you can use 'n comma delimited string of emails
Refer to http://php.net/manual/en/function.mail.php
Simply pass in the $emails variable to the mail() function as the first parameter.
so it would be:
$emails = 'email1#example.com,email2#example.com';
$message = 'send this to all email above';
$subject = 'the subject';
$headers = 'From: multiplememail#gm.Com';
mail($emails, $subject,$message, $headers);
I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
Hello.
I have contact form. I'd like to add CC to e-mail: abc#abc.de and change e-mail sender. Currently it shows my server as a sender, I'd like to have reply-to form users.
<?php
session_start();
//Ajax Questions Form
if(isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$arrival = $_POST['arrival'];
$departure = $_POST['departure'];
/// $adults = $_POST['adults'];
// $children = $_POST['children'];
// $room = $_POST['room'];
$requests = $_POST['requests'];
$to = 'contact#test.camp'; //Replace with recipient email address
$subject = 'Hotel Booking'; //Subject line for emails
$message = 'From: '.$name."\r\n".'Email: '.$email."\r\n".'Arrival: '.$arrival."\r\n".'People: '.$departure; //."\r\n".'Adults: '.$adults."\r\n".'Children: '.$children."\r\n".'Room: '.$room."\r\n".'Requests: '.$requests;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
}
//Contact Php Form
if(isset($_POST['contact_email'])){
$contact_name = $_POST['contact_name'];
$email = $_POST['contact_email'];
$contact_message = $_POST['message'];
$to = 'marek#gmail.com'; //Replace with recipient email address
$subject = 'Contact Form'; //Subject line for emails
$message = 'From: '.$contact_name."\r\n".'Email: '.$email."\r\n".'Message: '.$contact_message;
// Mail Functions
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // This line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
}
}
?>
The php mail function does not have much functionality try using something like PHPMailer which allows you to send more complex emails
For add CC or BCC or ReplyTo add header to your email structure :
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'To: andreas<mail1#gmail.com>, thomas<mail2#gmail.com>';
$headers[] = 'From: from <from#gmail.com>\r\nReply-to: <ReplyTo#gmail.com>';
$headers[] = 'Cc: Cc#gmail.com';
$headers[] = 'Bcc: Bcc#gmail.com';
mail($to, $subject, $message, implode("\r\n", $headers));
I have a php file which sends an email after a contact form has been submitted.
The email delivers correctly to the recipient, but it does never deliver to the Cc and Bcc. In the received email, anyways, the Cc is there(of course Bcc doesnt show up but if Cc is there Bcc should be too), but it just does not deliver to them.
Here is the code
<?php
$from = 'xx#xxx.com';
$to = $_POST['mail'];
$name = $_POST['nombre'];
$lastname = $_POST['apellido'];
$msg = "this is the content of the mail";
$subject = 'this is the subj';
$headers = "From:".$from."\r\n";
$headers .= "Cc:".$from.", xxx#xxx.com,xxy#xxy.com\r\n";
$headers .= "BCC:xyx#xyx.com\r\n";
if ($name=="" || $lastname=="" || $_POST['mensaje']=="" || $to==""){
echo '0';
}else{
if(mail($to, $subject, $msg, $headers)){
echo '1';
}else{
echo '-1';
}
}
?>
iv added the header field in the form code but when i receive the email, the "from" field in the email shows a garbled code and not the email address of the person who sent the message. This is my message processing code:
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $nome <$mail>\r\n";
$to = $youremail;
$mailsubject = 'Message received from'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is '.$fname.' '.$lname.'
Address: '.$address.'
'.$city.', '.$zip.', '.$country.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You declared your ^From: header` here:
$headers = "From: $nome <$mail>\r\n";
But it isn't used when you call mail()
mail($to, $subject, $body);
You need to pass $headers as fourth parameter. Else the headers won't take effect
mail($to, $subject, $body, $headers);
You haven't declared $nome anywhere in your code nor $mail in:
$headers = "From: $nome <$mail>\r\n";
And besides that, as this comment mentions, you didn't pass the header into the mail function. See the fourth parameter of the mail function.