I want to script a Newsletter.
With mysql_connect the newsletter does work; Email received and my echo string with listed emails also work.
If I want to translate it to PDO, any email is sent and no string to see, also i do not get any error message. (German strings in script)
<?php
mysql_connect('localhost', 'name', 'pw')
or die ("Verbindung fehlgeschlagen");
mysql_select_db('***')
or die ("Datenbank nicht gefunden");
$betreff = 'Newsletter';
$nachricht = 'Sehr geehrte/geehrter ##anrede## ##vorname## ##nachname##,' . "\n\n" .
'der neue Newsletter ist da.';
$header = 'FROM: newsletter#test.abc' . "\r\n" .
'Replay-To: anfrage#test.abc';
$sql = "SELECT * FROM newsletter";
$erg = mysql_query($sql);
$sqlemail = "SELECT email FROM newsletter";
$result = mysql_query($sqlemail);
while($row = mysql_fetch_object($result)) {
$email = $row->email;
foreach ($row as $email) {
mail($email, $betreff, $nachricht, $header);
echo "Email versendet an $email <br />";
}
}
?>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=***', 'name', 'pw');
$betreff = 'Newsletter';
$nachricht = 'Sehr geehrte/geehrter ##anrede## ##vorname## ##nachname##,' . "\n\n" .
'der neue Newsletter ist da.';
$header = 'FROM: newsletter#test.abc' . "\r\n" .
'Replay-To: anfrage#test.abc';
$sql = "SELECT * FROM newsletter";
$erg = mysql_query($sql);
foreach ($pdo->query($sql) as $row) {
$email = $row['email'];
mail($email, $betreff, $nachricht, $header);
echo "Email versendet an $email <br />";
}
?>
In the pdo side, you need to fetch the query as well. Do this:
$pdo->query($sql)->fetchAll()
Related
So I would love my code to update merchants that register on my site when a product is ordered from them and at the same time send the same email to our company email address.
While the code is being sent to our company email address, the mail to the merchant is not sending.
Could you please show me where I could be wrong? Thank you.
PLEASE NOTE THAT the email variable belongs to the merchant and is mostly a gmail account. Could this be a restriction from gmail or what?
function mail_merchants_and_shoply($cart_code){
$conn = $this -> connection;
$merchant_sent = false;
$shoply_sent = false;
$query = mysqli_query($conn, "SELECT * FROM business_account ORDER BY business_id DESC")or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query)){
$id = $row['business_id'];
$stat = "NEW";
$email = $row['email_add'];
$name = $row['business_name'];
$c_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE business_id = '$id' AND cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$merchant_mail_body = "<h2>NEW ORDER</h2><ul>";
while($arr = mysqli_fetch_array($c_query)){
$p_id = $arr['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$merchant_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$merchant_mail_body .="</ul>";
$mail_subject = "NEW SHOPLY ORDER";
// $mailHead = implode("\r\n", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
$mailHead = "MIME-Version: 1.0" . "\r\n";
$mailHead .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$mailHead .= 'From: Shoply<orders#shoply.ng>' . "\r\n";
if(mail($email, $mail_subject, $merchant_mail_body, $mailHead)){
$merchant_sent = true;
}
$s_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_query = mysqli_query($conn, "SELECT * FROM orders WHERE order_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_arr = mysqli_fetch_array($o_query);
$location = $o_arr['delivery_location'];
$phone = $o_arr['delivery_phone'];
$user_fname = $_SESSION['user_fname'];
$shoply_mail_body = "<h2>NEW ORDER</h2> FROM: $user_fname <ul>
<li><b>Address:</b> $location</li>
<li><b>Phone Number: </b>$phone</li>";
// $f_arr = mysqli_fetch_array($s_query);
// $customer_id = $f_arr['customer_id'];
// $c_query = mysqli_query($conn, "SELECT * FROM customers WHERE customer_id= '$customer_id'")or die(mysqli_error($conn));
// $c_arr[]
while($row = mysqli_fetch_array($s_query)){
$p_id = $row['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$shoply_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$shoply_mail_body .="</ul>";
$email = "sales#shoply.ng";
$mail_subject = "NEW SHOPLY ORDER";
// $mail_head = implode("\r\n", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Shoply<orders#shoply.ng>' . "\r\n";
if(mail($email, $mail_subject, $shoply_mail_body, $headers)){
$shoply_sent = true;
}
if($merchant_sent && $shoply_sent){
return true;
}else{
return false;
}
}
}
I recommend using php mailter instead of the built in mail function.
PHP's mail() function uses the server's basic mail server and on shared hosts these generally have a terrible email reputation and as such are blockedby many recipient servers. PHP Mailer allows you to communicate directly with any external web server, even via Gmail if you wish to send emails.
https://github.com/PHPMailer/PHPMailer
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();
?>
I added a cart function to this website, before It would only send one email after purchase because there was only one item at a time being purchased, now that I have added the cart function it needs to send one email instead of multiple emails.
This is the code I am using for processing the items purchased.
EDIT: Updated code
$item = array();
$size = array();
$color = array();
$price = array();
$querys = "SELECT * FROM ".$_SESSION["username"];
$ress = mysqli_query($connection, $querys);
if(mysqli_num_rows($ress) > 0){
while($rows = $ress->fetch_assoc()){
$bid = $rows["itemid"];
$description = $_POST["description"];
$bitem = $rows["itemname"];
$bsize = $rows["size"];
$bcolor = $rows["color"];
$bprice = $rows["price"];
$bcategory = $rows["category"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$company = $_POST["company"];
$address = $_POST["address"];
$suite = $_POST["suite"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$country = $_POST["country"];
$saddress = $_POST["saddress"];
$ssuite = $_POST["ssuite"];
$scity = $_POST["scity"];
$sstate = $_POST["state"];
$szip = $_POST["szip"];
$scountry = $_POST["scountry"];
$phone = $_POST["phone"];
$fax = $_POST["fax"];
$email = $_POST["email"];
$website = $_POST["website"];
$creditcard = $_POST["creditcard"];
$exdate = $_POST["experationdate"];
$cvv = $_POST["cvv"];
$create_date = date( 'Y-m-d H:i:s' );
$str_bitem = str_replace("'", "''", $bitem);
array_push($item, $bitem);
array_push($size, $bsize);
array_push($color, $bcolor);
array_push($price, $bprice);
$gw = new gwapi();
$gw->setLogin("demo", "password");
$gw->setBilling($firstname, $lastname, $company, $address, $suite, $city, $state, $zip, $country, $phone, $fax, $email, $website);
$gw->setShipping($firstname, $lastname, $company, $saddress, $ssuite, $scity, $sstate, $szip, $scountry, $email);
$gw->setOrder($bid, $description, 0, 0, 0, $_SERVER["REMOTE_ADDR"]);
$gw->doSale($bprice, $creditcard, $exdate, $cvv);
if($gw->responses['response'] == 1){
$firstname = $_SESSION["firstname"];
$lastname = $_SESSION["lastname"];
$query = "INSERT INTO orders (item_name, item_id, item_size, item_color, payer_email, first_name, last_name, address_name, address_city, address_state, address_zip, amount, country, create_date, shipped)
VALUES ('$str_bitem', '$bid', '$bsize', '$bcolor', '$email', '$firstname', '$lastname', '$saddress', '$scity', '$sstate', '$szip', '$bprice', '$scountry', '$create_date', 0)";
if(mysqli_query($db, $query)){
if($bsize=="N/A" && $bcolor=="N/A"){
define('DB_SERVER1', 'localhost');
define('DB_USERNAME1', 'grampmkn_gramsandpops');
define('DB_PASSWORD1', 'Grams123');
define('DB_DATABASE1', 'grampmkn_shop');
$db1 = mysqli_connect(DB_SERVER1,DB_USERNAME1,DB_PASSWORD1,DB_DATABASE1);
$sqlquery = "UPDATE `".$bcategory."` SET sold='1' WHERE id='$bid'";
if(mysqli_query($db1, $sqlquery)){
echo "Worked!";
}else{
echo "Error! : ";
echo mysqli_error($db1);
}
}else{
echo $bsize;
echo ":::::::";
echo $bcolor;
$conn = mysqli_connect('localhost', 'grampmkn_gramsandpops', 'Grams123', 'grampmkn_shop_quantity');
$query = "SELECT quantity FROM `".$bitem."` WHERE size='$bsize' AND color='$bcolor'";
$result = $conn->query ($query) or die($conn->error);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$q = --$row["quantity"];
$query = "UPDATE `$bitem` SET quantity='$q' WHERE size='$bsize' AND color='$bcolor'";
if(mysqli_query($conn, $query)){
}else{
echo "TESTHING1";
echo mysqli_error($conn);
}
}
}else{
$query = "SELECT quantity FROM `".$bitem."` WHERE size='$bsize'";
$result = $conn->query ($query) or die($db->error);
while($row = $result->fetch_assoc()){
$q = --$row["quantity"];
$query = "UPDATE `$bitem` SET quantity='$q' WHERE size='$bsize'";
if(mysqli_query($conn, $query)){
}else{
echo "TESTING";
echo mysqli_error($conn);
}
}
}
}
}else{
echo mysqli_error($db);
echo " ::::: ";
echo mysqli_error($connection);
}
$masked = str_pad(substr($creditcard, -4), strlen($creditcard), '*', STR_PAD_LEFT);
$transid = $gw->responses['transactionid'];
$username = $_SESSION["username"];
$query = "DELETE FROM $username WHERE itemid='$bid'";
if(mysqli_query($connection, $query)){
echo "Worked";
}else{
echo "Error! ";
echo mysqli_error($connection);
}
$query = "show tables;";
$tres = $connection->query($query) or die($connection->error);
if($tres->num_rows > 0){
while($trow = $tres->fetch_assoc()){
$table = $trow["Tables_in_grampmkn_cart"];
$sql = "DELETE FROM `".$table."` WHERE category='$table' AND itemid='$bid'";
if(mysqli_query($connection, $sql)){
echo "Deleted items from others carts!";
}else{
echo "There were no similar items in others carts!";
echo $table." : ";
echo $tcategory." : ";
echo mysqli_error($connection);
}
}
}
}else{
?>
<div class="alert alert-warning">
<strong>Error!</strong> Your card was declined!
</div>
<?php
}
}
$itemlist = implode(', ', $item);
$sizelist = implode(', ', $size);
$colorlist = implode(', ', $color);
$pricelist = implode(', ', $price);
if(!empty($bcolor)){
$to = $email;
$subject = "Purchase Confirmation";
$txt = "Thank you for your purchase of <br> ".$sizelist." ".$itemlist." - ".$colorlist."<br>Price: ".$pricelist."<br>Transaction ID: ".$transid."<br>CC: ".$masked."<br> <img src='https://gramsandpops.com/images/Logo.png'>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: info#gramsandpops.com' . "\r\n" .
'Reply-To: info#gramsandpops.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to,$subject,$txt,$headers)){
}
}else{
$to = $email;
$subject = "Purchase Confirmation";
$txt = "Thank you for your purchase of <br> ".$sizelist." ".$itemlist."<br>Price: ".$pricelist."<br>Transaction ID: ".$transid."<br>CC: ".$masked."<br> <img src='https://gramsandpops.com/images/Logo.png'>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: info#gramsandpops.com' . "\r\n" .
'Reply-To: info#gramsandpops.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to,$subject,$txt,$headers)){
}
}
$to = "gramsandpopsblueridge#gmail.com";
$subject = "Purchase Order";
$txt = "Item: ".$itemlist."\n Size: ".$sizelist."\n Color: ".$colorlist."\n Price: ".$pricelist."\n Address: ".$saddress.
" ".$scity.", ".$sstate." ".$szip."\n Name: ".$firstname." ".$lastname."\n CC: ".$masked;
$headers = "From: admin#gramsandpops.com" . "\r\n" .
"CC: admin#gramsandpops.com";
if(mail($to,$subject,$txt,$headers)){
}
?>
<div class="alert alert-success">
<strong>Success!</strong> Your transaction was successfully processed! An email confirmation will be sent shortly.
</div>
<?php
}else{
echo mysqli_error($connection);
}
I have written some code to make an email automatically send to the email address of the user at the click of a button. But for some reason is it not sending any emails, I have tested it with two different email addresses.
Code for button:
form action="sendellie.php" method="post">
<input type="submit" value="Buy tickets"/>
</form>
Code for processing page:
<?php
session_start();
include 'connect.php';
ini_set("sendmail_from", "********");
$user_id= $_SESSION['id'];
$sql = "SELECT username FROM user WHERE user_id = '$user_id'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uname = mysql_fetch_array($result);
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);
$user_id= $_SESSION['id'];
$sql = "SELECT email FROM user WHERE user_id = '$user_id'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);
while($row = mysql_fetch_array($result))
{
$name = $uname['username'];
$email = $uemail['email'];
$message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
$subject = $uname['username'];
echo "sent to"." ".$email." ".$subject." ".$message."<p />";
mail($email, $subject, $message);
}
echo "Go check your mail box:";
include 'close.php';
?>
<?php echo $uemail['email'] ?><br>
Back
This should work (check notes below):
<?php
session_start();
include 'connect.php';
ini_set("sendmail_from", "********");
$user_id= $_SESSION['id'];
$sql = "SELECT username FROM user WHERE user_id = '$user_id'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uname = mysql_fetch_array($result);
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);
$user_id= $_SESSION['id'];
$sql = "SELECT email FROM user WHERE user_id = '$user_id'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$uemail = mysql_fetch_array($result);
while($row = mysql_fetch_array($result))
{
$name = $uname['username'];
$email = $uemail['email'];
$youremail = "youremail#mail.com";
$yoursubject = " Subject of the email";
$message = "This email is to confirm you have purchased one Ellie Goulding Ticket for the O2";
$subject = $uname['username'];
mail( $email , $yoursubject, $message, "From:" . $youremail);
echo "sent to"." ".$email." ".$yoursubject." ".$message."<p />";
}
echo "Go check your mail box:";
include 'close.php';
?>
<?php echo $uemail['email'] ?><br>
Back
Notes:
The PHP mail() Function
Syntax
mail(to,subject,message,headers,parameters)
I use this code to send mail try this
<html>
<body>
<?php
if (isset($_REQUEST['email']))
{
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone#example.com", $subject,
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
</body>
</html>
I am attempting to setup a mail script which will first run a simple select from mysql, and use these array variables in the message. However all the variables are not output to the message body, only one row of variables. Here is my script:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
}
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
appreciate any insight anyone cares to share on this.
Thanks,
--Matt
Try this instead:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink - > query($sql1);
while ($row1 = $result1 - > fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:".$from;
mail($to, $subject, $message, $headers);
}
This will do a mail per row.
thats because you're while keeps overwriting the variables, so it'l get only the last one. You might want to just build the $message variable in the while loop by doing. that will send 1 email with everything
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name"
}
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
You are overwriting your variables in the loop so $name, etc. will contain the values found in the last row after the loop.
What you could do is construct your message in the loop:
$message = '';
while($row1 = $result1->fetch_assoc()) {
$message .= $row1[...]; // whatever you need
}
Try this:
$message = NULL;
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name" }
And remove the other $message variable under the loop. Notice the . before the = in the $message. This tells php to continually add on to the $message
That's because you're assigning the variables in the loop, but using them in the $message variable outside the loop. So your $message contains only the items from the last row/record. Try moving and appending values in the $message variable inside the while loop.
So it could be
while($row1 = $result1->fetch_assoc()) {
//assign vars here
$message .= "$name $tape_no $date_out $member_name\n";
}
$message = "$message
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
//assign other variables
//mail()
Hope that helps.