PHP auto send email not functioning - php

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>

Related

Newsletter works with mysql_connect but not with PDO

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()

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";
}

Sending password to mobile

I am using the below code to send password to mobile and the code doesn't seems to work.
Really appreciate any help on this.
if (isset($_REQUEST['submit'])) {
$phone = "select mobno from registration where username='" . $_GET['id'] . "' ";
$mailto = "select regemail from registration where username='" . $_GET['id'] . "' ";
$subject = "passcode";
mail($mailto, $subject, $);
echo '<script type="text/javascript">alert("Code Sent Successfully");</script>';
echo '<meta http-equiv="refresh" content=",log.php">';
}
Try editing your code like this:
if (isset($_REQUEST['submit'])) {
$phone = mysql_query("SELECT mobno FROM registration WHERE username='" . $_GET['id'] . "' ; ");
$mailto = mysql_query("SELECT regemail FROM registration WHERE username='" . $_GET['id'] . "' ; ");
$subject = "passcode";
mail($mailto, $subject, $phone);
echo '<script type="text/javascript">alert("Code Sent Successfully");</script>';
echo '<meta http-equiv="refresh" content="0,log.php">';
}
Don't forget to connect to your mySQL-Database at the beginning!
Hope this helps.
/* DB connection */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
if (isset($_REQUEST['submit'])) {
$sql = 'SELECT mobno,regemail FROM registration WHERE username=:user'; // I suspect this should be id=:user due to $_GET['id'] and not $_GET['username']
$sth = $dbh->prepare($sql);
$sth->execute(array(':user' => $_GET['id']));
$data = $sth->fetch(PDO::FETCH_ASSOC);
$phone = $data['mobno'];
$mailto = $data['regemail'];
$subject = "passcode";
mail($mailto, $subject, $phone);
echo '<script type="text/javascript">alert("Code Sent Successfully");</script>';
echo '<meta http-equiv="refresh" content=",log.php">';
}
You may find the php manual on PDO helpful http://www.php.net/manual/en/class.pdo.php

Simple query not running

First off, I am aware I am open to SQL injection, this is just a prototype. But it still should be working.
For the life of me I can't figure out why I can't pull an item out of my array. What could I possibly be doing wrong? I've been fiddling with this seemingly simple query for way too long and I can't seem to get it to pull out data. I feel like it is something so simple....
$query = 'SELECT * FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$ID = $row['ID'];
I am getting no result for $ID ....
Here is my entire code:
<html>
<head>
<?php
$email = $_GET["email"];
$servername="localhost";
$username="*****";
$password="*****";
$database="*****";
$conn= mysql_connect($servername,$username,$password)or die(mysql_error());
mysql_select_db("$database",$conn);
$query = 'SELECT email FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
//Checks if the email address exists in the system already
if (mysql_num_rows($result) ) {
die("Duplicate email found!");
}
else {
//use current date/time combination times the number 11 times the ID to get a unique confirmation number.
$query = 'SELECT * FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$ID = $row['ID'];
echo $row;
$date = date("mydhis");
$date2 = $date * 11 * $ID;
echo $ID . " <-> " . $date . " <-> <p>" . $date2;
$sql="insert into users (first,last,displayname,email,password,verification_email)values('$_GET[first]','$_GET[last]','$_GET[display]','$_GET[email]','$_GET[password]','$date2')";
$result=mysql_query($sql,$conn) or $string = mysql_error();
$confirmlink = "http://www.somewebsite.com/android/confirm.php?" . $date2;
$to = $_GET['email'];
$subject = "Thank you for Registering!";
$message = "Hello " . $_GET['display'] . " and thank you for registering with the Smeet app! To confirm your email address (and let us know you aren't a bot), please click the following link: " . $confirmlink;
$from = "noreply#smeet.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers) or die('You have successfully registered however mail servers are currently down, you may or may not receive a confirmation email');
print "<h1>You have registered successfully</h1>";
print "You will receive an email shortly with instructions on how to confirm your email address.</a>";
}
?>
</body>
</html>
Thanks for any help at resolving this.
It was a simple answer and I figured it out!
My $ID was being pulled before the record was created, that's why it was blank! Dumb mistake on my part.

Emails via script get set out 3 times?

I have a email script that runs every 15 minutes and is supposed to send a email once using PHP mailer. For some reason, it's sending out 3 emails a time.
Here's my code:
<?php
// Database connect
include("class.phpmailer.php");
$sql2 = "SELECT * FROM eblast_email WHERE id = '1'";
$result2 = mysql_query($sql2);
while ($myrow2 = mysql_fetch_array($result2)){
$get_event_id = "".$myrow2['event_id']."";
$mail = new PHPMailer();
// Login information here
$mail->Subject = "Subject here";
$html.= "HTML Message here";
$plain = "Plain Message here";
$mail->Body = $html;
$mail->AltBody = $plain;
$sql = "SELECT * FROM email_users WHERE sent = 'no' LIMIT 0, 40";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)){
$email_to_send_to = "".$myrow['email']."";
$rsvp_check = mysql_query("SELECT * FROM event_members WHERE event_attending='$get_event_id' AND email='$email_to_send_to'");
$rsvp_check_done = mysql_num_rows($rsvp_check);
if ($rsvp_check_done == 0) {
$mail->AddAddress($email_to_send_to);
if(!$mail->Send()) {
echo "<b>Error sending email to " . $myrow['email'] . ". </b>" . $mail->ErrorInfo;
echo "<br>";
} else {
echo "Message to " . $myrow['email'] . " has been sent.<br>";
}
mysql_query("UPDATE email_users SET sent='yes' WHERE email='".$myrow['email']."'") or die (mysql_error());
$mail->ClearAddresses();
} else {
mysql_query("UPDATE email_users SET sent='yes' WHERE email='$email_to_send_to'") or die (mysql_error());
echo "$email_to_send_to has already registered ($rsvp_check_done) -- $get_event_id && $email_to_send_to<br>";
}
sleep(2);
}
}
echo "<br>Done.";
?>
Modify the code to have write to a log file every time it runs. My guess is that it's just getting called 3 times.
Example:
file_put_contents("log.txt", $_SERVER['REQUEST_TIME'] . "\n", FILE_APPEND);

Categories