PHP: Send more than article in one email - php

I have a table named 'an_posts' and a column inside the table named 'post_title'. I need to send an email to user when i select more than 'post_title'. But the code below is only send one 'post_title'. Why is that happened and what I have to add in the code so that the email send will retrieve two or more data from database ?
if($_GET["action"] == 'sendpostnewsletter')
{
global $post;
$post_id = $_POST["post_id"];
$emailtosender = $_POST["emailtosender"];
$subjecttitle = $_POST["subjecttitle"];
$sql = "select * from an_posts where post_type = 'post' and post_status = 'publish' and ID in ($post_id)";
$formssql = mysql_query($sql);
while ( $row = mysql_fetch_array($formssql) )
{
$message = '
<div align="center"><h3>'.$row['post_title'].'</h3></div>
<br />
<div style="text-align:justify; font-size:16px; font-family:Verdana, Geneva, sans-serif; font-weight:bold">
'.$row['post_content'].'
</div>';
}
$message .= "<hr />";
$headers = "From: Newsletter <list#domainname.com>" . "\n" ;
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\n";
wp_mail( $emailtosender , $subjecttitle , $message, $headers );
$result='<div id="message" class="updated fade"><p><strong><h5>done</h5></strong></p></div>';
echo $result;
}

You are overwriting the content of message each loop. Instead append to it:
//initialize outside the loop
$message='';
while ( $row = mysql_fetch_array($formssql) )
{
//then append - notice the dot .=
$message .= '
...

Related

Sending an email to 2 variables in PHP

Im trying to send an email to 2 variables currently I have tried to add 3 emails to an array and put that into a variable and get the other email from the form, I then dont know how to put both of those variables together when sending so they each have their own "to" but this does not work or something doesnt and I dont know what?? And yes I have real emails that I use these are placeholders for this!!!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit'])) {
$email = $_POST['LOGSE'];
$email2 = array("test#test.com", "test#test.com", "test#test.com");
$name = $_POST['full_name'];
$eventtitle = $_POST['EventT'];
$InCharge = $_POST['InCharge'];
$Venue = $_POST['Venue'];
$VenY = $_POST['VenR'];
if($VenY != "Yes"){
$ava = " Have not checked if";
}
else{
$ava = "";
}
$dates = $_POST['dateS'];
$datee = $_POST['dateE'];
$adults = $_POST['Adults'];
$children = $_POST['Children'];
$catreq = $_POST['CateReq'];
if (catreq != ''){
$catreq = $catreq;
}
else{
$catreq = "No Catering Needed";
}
$logreq = $_POST['LogReq'];
if (logreq != ''){
$logreq = $logreq;
}
else{
$logreq = "No Logistic Equipment Needed";
}
$itreq = $_POST['ITReq'];
if (itreq != ''){
$itreq = $itreq;
}
else{
$itreq = "No IT Needed";
}
$tran = $_POST['TransR'];
if($tran != Yes){
$tran = "NO ";
}
else{
$tran = "";
}
$Risk = $_POST['RiskR'];
if($Risk != Yes){
$Risk = "NO ";
}
else{
$Risk = "";
}
$othern = $_POST['OtherN'];
// The Email:
$from = 'test#test.com';
$to = $email;
$to = $email2;
$subject = 'Event Form ' .$eventtitle;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'From: Event Form<test#test.com>' . "\r\n";
$headers .= 'Cc: test#test.com, test#test.com' . "\r\n";
$body = '<html>
<head>
<title>Event</title>
<style>
h1 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
h3 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
</style>
</head>
<body>
<h1>Event Details</h1>
<h3>Name: '.$name.'</h3>
<h3>Event Title: '.$eventtitle.'</h3>
<h3>Event Manager: '.$InCharge.'</h3>
<h3>Venue: '.$Venue.' - '.$ava.' Available</h3>
<h3>Date Start: '.$dates.'</h3>
<h3>Date End: '.$datee.'</h3>
<h3>Adults Attending: '.$adults.' Children Attending:
'.$children.'</h3>
<h3>Catering Requirements: '.$catreq.'</h3>
<h3>Logistic Requirements/Equipment: '.$logreq.'</h3>
<h3>IT Requirements: '.$itreq.'</h3>
<h3>Other Notes: '.$othern.'</h3>
<h3><font color="red">'.$tran.'</font>Transport Has Been
Booked</h3>
</body>
</html>' . "\r\n";
mail( $to, $subject, $body ,$headers );
/* echo "An Email Has Been Sent<br>Thank You."; */
header('Location: ./thanks.html');
}
}
The $to in mail function should be a string. If there are more than one email address then those should be comma separated. In your case you might need to do like following.
$to = $email . ','. implode(',', $email2);
Add email address with the comma
$email_to = "test#test.com,some#other.com,yet#another.net";
Also you can add
$headers = "Bcc: someone#domain.com";
Use a foreach on the array:
foreach($email2 as $to){
mail( $to, $subject, $body ,$headers );
}
Or like others are suggesting:
$to = $email.",".implode(",",$email2);
That doesn't make sense:
$to = $email;
$to = $email2;
Parmaeter $to should be a string of comma-seperated email-addresses, e.g.:
$to = $email1 . ", " . $email2 . ", " . $email3;

Send over 2000 e-mails with PHP mail function

I am trying to create a system that sends an e-mail to everyone of the users of my webpage.
It seems though that there are too many emails - about 2400 emails, to send for it to work.
Is there a smarter way, or a way to send 10 at a time or something like that?
Here is my code:
<?php
include('../../includingThis.php');
$fra = "ME <me#me.com>";
$emne = "";
$sql = "SELECT * FROM sendThese WHERE id=1";
if ($result = mysqli_query($con, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
$emne = $row["subject"];
$message = $row["message"];
$mailsHere = $row["mailsHere"];
$til = explode(",", $mailsHere);
}
}
$besked = file_get_contents('sendThese.html');
$besked = str_replace("%%message%%", $message, $besked);
// HEADERS
$headers = "MIME-Version:1.0\r\n";
$headers .= "Content-type: text/html; charset=UFT-8\r\n";
$headers .= "From: ".$fra."\r\n";
// Mail til ejeren af siden
$mailsSend = 0;
foreach ($til as $value) {
mail($value,$emne,$besked,$headers);
$mailsSend++;
echo $value . "<br>";
sleep(2);
}
// Sender vi brugeren videre til en side
//header("Location: index.php");
echo "Done - " . $mailsSend . " e-mails sendt.";
die();
?>

how to send an emails to a list while updating MYSQL Database

i have wrote a php script to send an emails.In this script it checks whether current date is equal with the emailfly_date and email is = o.if that satisfies email will sent to the user.this script is working for one user at a one time.that if database has five records that that matches current criteria it sends the email only to the last id.what is the reason for this?
id
250
251
252
253
it will send the email only to the ID 253.but not to the others.
Here is mycode
<?php
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE reqnum = '' AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$regid = $row['ID'];
$activemail= $row['nowtime'];
/*echo $regid . '<br />';
echo $activemail. '<br />';*/
}
$query = mysql_query("SELECT ID, email, reqnum, emailfly_date, email_sent FROM xxx WHERE ID = '$regid' AND emailfly_date = '$activemail'");
$rowdata = mysql_fetch_array($query);
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
if ($reqnum != '') {
/* echo "not sucess";*/
} elseif ($email_sent == '1') {
/* echo "email already sent";*/
} elseif ($email_sent == '1' && $reqnum != '') {
/* echo "no way to send";*/
}
elseif($email_sent == '0' && $reqnum == '' ){
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$regid'");
echo "sucess";
}
else{
echo "bye";
}
?>
From $query = mysql_query(.....bla...bla... to last else{
echo "bye";
} Put all this inside your while loop.
Also if you fetch data from same table try to use single query.
mysql_() is depreciated now. So try to use mysqli_()
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE id in ('250','251','252','253') AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while($rowdata = mysql_fetch_array($result))
{
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$ID'");
echo "sucess";
}

HTML email doesn't work

I am submitting a form which inserts record into database and then sends email to the specified address. The below code doesn't working for me. i'm getting errors.
if (isset($_POST['submit'])) {
//if (
// !empty($_POST['item_cid']) &&
// !empty($_POST['item_code']) &&
// !empty($_POST['item_name']) &&
// !empty($_POST['item_price']) &&
// !empty($_POST['item_qty']) &&
// is_array($_POST['item_cid']) &&
// is_array($_POST['item_code']) &&
// is_array($_POST['item_name']) &&
// is_array($_POST['item_price']) &&
// is_array($_POST['item_qty']) &&
// count($_POST['item_cid']) === count($_POST['item_code'])
// )
//{
foreach($_POST['item_cid'] as $key => $value) {
//Data for Orders Table
$cid = mysqli_real_escape_string($connection,$value);
$pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
$pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
$pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
$pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);
//Data for Customers Table
$cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]);
$cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]);
$cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]);
$caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]);
$ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]);
// $sql = "INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty) VALUES ('$value', '$pcode', '$pname', '$pprice', '$pqty')";
// $sql2 = "INSERT INTO customers (cid, cname, cemail, cphone, caddress, ctotal) VALUES ('$value','$cname','$cemail','$cphone','$caddress','$ctotal')";
if ($connection->query($sql) === TRUE) {
echo "Orders record created successfully \n";
}
// } else {
// echo "Error: " . $sql . "<br>" . $connection->error;
// }
if ($connection->query($sql2) === TRUE) {
echo "Customers record created successfully \n";
}
// } else {
// echo "Error: " . $sql2 . "<br>" . $connection->error;
} // close the loop
//********************************
// START EMAIL FUNCTION
//********************************
// PREPARE THE BODY OF THE MESSAGE
$message = '<html><body>';
$message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />';
$message .= '<h3>Customer Information:</h3>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr><td><strong>Name:</strong></td><td>'. strip_tags($_POST['item_cname']) .'</td></tr>';
$message .= '<tr><td><strong>Email:</strong></td><td>'. strip_tags($_POST['item_cemail']) .'</td></tr>';
$message .= '<tr><td><strong>Phone:</strong></td><td>'. strip_tags($_POST['item_cphone']) .'</td></tr>';
$message .= '<tr><td><strong>Address:</strong> </td><td>'. strip_tags($_POST['item_caddress']) .'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
// MAKE SURE THE "FROM" EMAIL ADDRESS DOESN'T HAVE ANY NASTY STUFF IN IT
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
if (preg_match($pattern, $_POST['item_cemail'])) {
$cleanedFrom = $_POST['item_cemail'];
} else {
return "The email address you entered was invalid. Please try again!";
}
// CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'info#domain.com';
$subject = 'New order Arrived CustomerID #'.$cid.' ';
$headers = "From: " . $cleanedFrom . "\r\n";
$headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your order has been sent. Our sales department will contact you soon...';
} else {
echo 'There was a problem sending the email.';
}
print_r($_POST['item_cname']);
} // Data Inserted & Emailed Close IF Statement
session_destroy();
The HTML
<p class="form-row">
<label class="" for="item_cname[]">Your Name <span class="required">*</span></lable>
<input class="input-text" type="text" name="item_cname[]" placeholder="Your Name" />
</p>
<p class="form-row">
<label class="" for="item_cemail[]">Email Address <span class="required">*</span></lable>
<input type="text" name="item_cemail[]" placeholder="Your Email Address"/>
</p>
<p class="form-row">
<label class="" for="item_cphone[]">Phone Number <span class="required">*</span></lable>
<input type="text" name="item_cphone[]" placeholder="Your Phone Number"/>
</p>
<p class="form-row">
<label class="" for="item_caddress[]">Address <span class="required">*</span></lable>
<textarea name="item_caddress[]" placeholder="Your Address" class="input-text" rows="2" cols="2" maxlength="140"></textarea>
</p>
The Errors
Notice: Undefined variable: sql in /home/public_html/dev/process.php
on line 48
Warning: mysqli::query(): Empty query in
/home/public_html/dev/process.php on line 48
Notice: Undefined variable: sql2 in /home/public_html/dev/process.php
on line 55
Warning: mysqli::query(): Empty query in
/home/public_html/dev/process.php on line 55
Warning: strip_tags() expects parameter 1 to be string, array given in
/home/public_html/dev/process.php on line 72
Warning: strip_tags() expects parameter 1 to be string, array given in
/home/public_html/dev/process.php on line 73
Warning: strip_tags() expects parameter 1 to be string, array given in
/home/public_html/dev/process.php on line 74
Warning: strip_tags() expects parameter 1 to be string, array given in
/home/public_html/dev/process.php on line 75
Warning: preg_match() expects parameter 2 to be string, array given in
/home/public_html/dev/process.php on line 81
All those errors pointing towards the strip_tags($_POST['item_*']) (the * represents the name of item).
How to fix this?
Are you looking for this. If yes, this should work for you:
<?php
if (isset($_POST['submit'])) {
foreach($_POST['item_cid'] as $key => $value) {
//Data for Orders Table
$cid = mysqli_real_escape_string($connection,$value);
$pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
$pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
$pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
$pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);
//Data for Customers Table
$cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]);
$cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]);
$cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]);
$caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]);
$ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]);
if ($connection->query($sql) === TRUE) {
echo "Orders record created successfully \n";
}
if ($connection->query($sql2) === TRUE) {
echo "Customers record created successfully \n";
}
}
$message = '<html><body>';
$message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />';
$message .= '<h3>Customer Information:</h3>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr><td><strong>Name:</strong></td><td>'. $cname .'</td></tr>';
$message .= '<tr><td><strong>Email:</strong></td><td>'. $cemail .'</td></tr>';
$message .= '<tr><td><strong>Phone:</strong></td><td>'. $cphone .'</td></tr>';
$message .= '<tr><td><strong>Address:</strong></td><td>'. $ctotal .'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
if (preg_match($pattern, $cemail)) {
$cleanedFrom = $cemail;
} else {
return "The email address you entered was invalid. Please try again!";
}
$to = 'info#example.com';
$subject = 'New order Arrived CustomerID #'.$cid.' ';
$headers = "From: " . $cleanedFrom . "\r\n";
$headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your order has been sent. Our sales department will contact you soon...';
} else {
echo 'There was a problem sending the email.';
}
print_r($_POST['item_cname']);
} // Data Inserted & Emailed Close IF Statement
session_destroy();
?>
Probably one of your post data is array object.
Try to make print_r($_POST[xxx]) to check your POST values.
Strip Tags function parameter should not be an array object.

Email is only being sent once

I had asked a question yesterday about sending an email every three days which was answered so I'm not sure if I should create a new one or add to the old.
The check is working fine as I get the correct ouptut on screen, but when I try to send the email it is only sending one result. I've tried foreach loops, putting the mail function in different places none of which seem to work. My code is below, I've removed most of the message parts as they are just long tables.
**UPDATE ****
I've added the recipients array into the while loop and also set $subject and $headers to empty values before adding values. This has worked.
$sql = "SELECT * FROM bookings " .
"WHERE DATE(date) > DATE(NOW()) " .
"AND dateofquote != '' " .
"AND email != '' " .
"AND confirmed = 0";
$result = mysql_query($sql);
$num_rows = mysql_numrows($result);
$today = date('Y-m-d');
if ($num_rows) {
while ($row = mysql_fetch_array($result)) {
$recipients = array(); // Updated
$id = $row['id'];
// rest of rows from db
$date_time1 = new DateTime($today);
$date_time2 = new DateTime($date_of_quote);
$interval = $date_time1->diff($date_time2);
$diff = $interval->format('%a');
if ($diff % 3 == 0) {
if ($type == 'W') {
$message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">';
// rest of table
echo '<h1>Weddding Email</h1>'.$message.'<br />End Wedding<br /><br /><hr>';
// tried to send email from here for this option
}
elseif ($type == 'D') {
$message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">';
// rest of table
echo '<h1>Debs Email</h1>'.$message.'<br />End Debs<br /><br /><hr>';
// tried to send email from here for this option
}
elseif ($type == 'CR') {
$message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">';
// rest of table
echo '<h1>Country Run Email</h1>'.$message.'<br />End Country Run<br /><br /><hr>';
// tried to send email from here for this option
}
elseif ($type == 'H') {
$message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">';
// rest of table
echo '<h1>Hourly Email</h1>'.$message.'<br />End Hourly<br /><br /><hr>';
// tried to send email from here for this option
}
} else {
echo 'something went wrong';
}
$recipients[] = $email;
$subject = ''; // Updated
$headers = ''; // Updated
$subject .= "An $type_value Enquiry has been received from company.ie";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Name <email#email.com>' . "\r\n";
foreach($recipients as $to){
if(mail($to, $subject, $message, $headers)) {
echo "E-Mail Sent to ";
echo $to.'<br />';
} else {
echo "There was a problem";
}
}
}
}
Try like this inside your while loop. Your mail content is inside the while loop, but you are trying mail function outside the while loop, so it is sending last content to all your mails in $recipients array.
$subject='';
$headers='';
$subject .= "An $type_value Enquiry has been received from company.ie";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Absolute Limos <email#email.com>' . "\r\n";
mail($email, $subject, $message, $headers);
your problem lies here:
$recipients[] = $email;
You create your email array outside your loop, thus you only get the last email address value.
Put it in your
while ($row = mysql_fetch_array($result)) {

Categories