I have a table below in MYSQL
Mmeber_Email---------------Member_Name------Data---
mike01#yahoo.com -------- Mike ---------- 100 ------
jacknick#gmail.com --------Jack -----------50 ----
jillwag#hotmail.com -------Jill ---------- 75 ------
jnash#gmail.com -------- John ---------- 10 ------
Now, I have managed to extract the data from the database using PHP.
but i don't know how to email the data of each member on their respective email using PHP Mail function.
How should i tackle the problem of sending emails to multiple recipients using the PHP mail function below i.e Mike Should get the email containing the message 100, Jack 5 and so on..
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = //data extracted from database using a common query comes here
$headers = "From: webmaster#example.com";
mail($to,$subject,$txt,$headers);
?>
You would need to loop through the results returned from MySQL. Here is an example:
while($row = mysqli_fetch_array($results)) { // start loop
$to = $row['member_email'];
$subject = 'the subject';
$message = "Dear ". $row['member_name'] . "\r\n";
$message .= "Here is your data: " . $row['member_data'] . "\r\n";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
The data from your database is contained in the $row array setup by the while{} loop.
Make sure to pay attention to the details for mail in the docs.
Set $to to a comma separated list of all the recipients:
$to = "somebody1#example.com, somebody2#example.com, somebody3#example.com";
Related
I've created a web site and I'm using this link for a JS pop up form to be emailed using PHP.
I also used the code from here and everything works except for a couple things. When I don't remove some variables, the order of information is out of place when it emails.
And when I keep all the variables, I get the following error in a log and nothing sends until I remove them:
PHP Warning: mail() expects at most 5 parameters, 7 given in xxxxxxxxxxxxxxxxxxxxx/quote.php on line 41
Below is the code where the error is coming from:
else{
$Company = $_POST['company'];
$Email = $_POST['vemail'];
$Name = $_POST['name'];
$Number = $_POST['number'];
$Info = $_POST['info'];
$headers = 'From:'. $email2 . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
// $message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("info#bvcdenver.com", $Company, $Email, $Name, $Number, $Info, $headers);
echo "Your quote request has been sent successfuly ! Thank you for your interst. You are being redirected back to xxxxxxxxxxxx in 5 seconds.";
}
How can I send all the variable? Order won't matter if I can get them all to send.
Note: I don't have a lot of scripting experience. This site is created using only HTML/CSS and these PHP and JS sections. So ideally I'd like to not change the entire site.
You need to proper use the php mail function() as it is stated here http://php.net/manual/en/function.mail.php.
The max number of parameters is 5:
- TO ( in your case: "info#bvcdenver.com" )
- SUBJECT ( in your case: $Company )
- MESSAGE ( in your case: $Email )
the last 2 are additional headers and additional parameters.
If you want to send all the data "email, name, number and info" you should organize a string/text variable and put it on the 3rd place
like:
$message = $Email . " - " . $Name . " - " . $Number . " - " . $Info;
mail("info#bvcdenver.com", $company, $message, $headers);
This should do the work and you can customize the message numbers how you want, with html or raw new lines, and get a proper template.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';//pass every variable into message
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I'm looking for a way to send 2 different email messages with different recipients.
I know that I can send the same message to a list of emails, but what I need is to send one text to certain recipients and other text to other list of emails.
I need this because my message contains approval information (which should only be seen by an admin) and I need to sent at the same time other mail just for telling the user "your request have been sent and will be reviewed".
Is there a function in mail() that can do this?
-As requested-
Unfortunately, PHP's mail() function can only handle this by using seperate mail() functions.
You can send the same email to multiple recipients at the same time, but to send two different messages (and two different subjects) intended for two different recipients requires using two different mail() functions, along with two different sets of recipients/subjects/messages/headers.
For example:
/* send to 1st recipient */
$to_1 = "recipient_1#example.com";
$from = "from_recip#example.com";
$subject_1 = "Subject for recipient 1";
$message_1 = "Message to recipient 1";
$headers_1 = 'From: ' . $from . "\r\n";
$headers_1 .= "MIME-Version: 1.0" . "\r\n";
$headers_1 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_1, $subject_1, $message_1, $headers_1);
/* send to 2nd recipient */
$to_2 = "recipient_2#example.com";
$from = "from_recip#example.com";
$subject_2 = "Subject for recipient 2";
$message_2 = "Message to recipient 2";
$headers_2 = 'From: ' . $from . "\r\n";
$headers_2 .= "MIME-Version: 1.0" . "\r\n";
$headers_2 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_2, $subject_2, $message_2, $headers_2);
Just like this.
<?php
//first mail ////
$to = '1stRe#example.com';
$subject = 'the subject';
$message = '1st Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//second mail ////
$to = '2ndRe#example.com';
$subject = 'the subject';
$message = '2nd Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I implemented this http://www.websitecodetutorials.com/code/jquery-plugins/jquery-ajaxsubmit.php tutorial on my form, and the form works successfully and displays the thank you div on submission. But the problem is I don't receieve any email, seems like PHP mailer is not working.
Please see the code below
<?php
// Insert your email/web addresses and correct paths
$mailto = 'adil#adilsaleem.co.uk' ;
$from = "web#city.com" ;
$formurl = "http://astonstorlin.co.uk/citycoaches3/formmail.php" ;
$errorurl = "http://astonstorlin.co.uk/citycoaches3/error.php" ;
$thankyouurl = "http://astonstorlin.co.uk/citycoaches3/thankyou.php" ;
// Place Your Form info here...
$pickuppoint = ($_POST['pickuppoint']);
$destination = ($_POST['destination']);
// Check If Empty
// Add more Validation/Cleaning here...
// Place your Corresponding info here...
$message =
"Pick Point: $pickuppoint\n\n" .
"Destination: $destination\n\n" .
"noppl: $noppl\n\n"
;
// Leave Alone
mail($mailto, $from, $message,
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep );
header( "Location: $thankyouurl" );
exit ;
?>
I think your headers are not working. $headersep is not defined and moreover please follow :
$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);
Example copied from http://php.net/manual/en/function.mail.php you can refer for more reading this link.
If you are on a shared hosting for example, sometimes they require you to use a fifth parameter like additional_parameters.
mail('nobody#example.com', 'the subject', 'the message', null,'-fwebmaster#example.com');
Check example 3 here: http://php.net/manual/en/function.mail.php
Thank you very much for all your help people, much appreciated.
I got the script working by deleting
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep );
And the script started working. I have tried using other HTML php mailer but them don't seem to work with this tutorial example for some reason.http://www.websitecodetutorials.com/code/jquery-plugins/jquery-ajaxsubmit.php
I'm using dreamhost for my mailing.
I'm having an issue with php mail function additional headers parameters.
This code works, and the email is sent:
$to = 'myemail#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $name <**webmaster#example.com**>\r\n" .
"Reply-To: $name <**webmaster#example.com**>\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
but when I replace webmaster#example.com to the variable $email
$headers = "From: $name <**$email**>\r\n" .
"Reply-To: $name <**$email**>\r\n" .
'X-Mailer: PHP/' . phpversion();
The email doesn't get sent. I did do a print_r($_POST), and the elements are there. I also did another test where I typed the email: webmaster#example.com into the form, to see if it would send, and it did. So my question is, how do I remedy this issue, if a user types their email address into the form with another mailing extension, that mail will not get sent, but if the extension is #example.com, then the mail will get sent.
$name variable should be the full emailaddress:
"$name = " 'a name' email#whatever.com> "
$headers = "From: $from \r\n";
works on my servers. Add the leading < to the email addtress. This system won't display the string at all if I include it. If your form has different variables for the responders nam and email address you'll have to concatenate them to get $name in the right formate.
I am using a simple HTML form for an event registration and I would like to send a PHP confirmation email to the registrant with the same information that is going to the administrator.
Here are the details:
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
I would like to use the value from $emailFrom in the following email address to:
$emailTo = "name#domain.com", \"$emailFrom\";
edit: Added new configuration:
$emailHeaders = 'From: "Conference" <noreply#conference.org>' . 'CC: . $emailFrom .';
This obviously doesn't work, but you get the idea of what I am trying to.
Thanks!
You will need to add headers to the mail function...
for example:
$headers = 'From: Family History Conference <noreply#familyhistoryconference.org>' . "\r\n" .
'Reply-To: noreply#familyhistoryconference.org' . "\r\n" .
'CC: ' . $emailFrom . "\r\n";
and then
mail($to, $subject, $message, $headers);
References:
PHP Mail Function
You put "From: $name <$emailFrom>" in the additional parameters of the mail() function:
http://php.net/manual/en/function.mail.php
So, basically you want to CC a copy of the message to the user as well as the administrator?
It depends on which PHP library you're using to send it. If you're using the default PHP mail() function, you'll need to add additional headers:
// Additional headers
$headers .= 'Cc: ' . $emailFrom . "\r\n";
// Mail it
mail('admin#email.com', $subject, $message, $headers);