PHP Send an Email using mail function [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email using php but unable to do it, whereas I am getting data into MySQL database.
I have posted my whole code, please check and let me know where's the mistake.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
mysql_close($objConnect);
echo json_encode($arr);

This code is really working ...
<?php
$to = 'to_email#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$errLevel = error_reporting(E_ALL ^ E_NOTICE); // suppress NOTICEs
if(!mail($to, $subject, $message, $headers)){
error_reporting($errLevel);
}
?>

You have not added mail() function in your code then how will it send the mail and to send email from localhost it should be properly configured. Below code will work on the server.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $message, $headers)){
echo 'Mail cant be send';
}
else
{
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
echo json_encode($arr);
}
mysql_close($objConnect);

Related

How to echo multiple rows using mail()?

I've got stuck trying to echo multiple rows from a MySql database into the $message part of the email. The attached code only returns one result.
I want to send 1 email, in the email I want to output the 10 rows from the SQL query.
I actually receive the same email ten separate times rather than one email with ten rows.
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$message = $row["Maxim"]. "</br>";
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Move the mail call out of the loop
// don't select all (*) fields if you only need one
$sql = "SELECT Maxim FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
$message = '';
while($row = $result->fetch_assoc()) {
$message .= $row["Maxim"]. "</br>";
}
// prepare email headers
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// then send
mail($to, $subject, $message, $headers);
I haven't tested the code but give this a try:
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$message = $row["Maxim"]. "</br>";
}
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
I have placed the to, subject, header and mail function out of the loop.
Cheers

Sending Email after sending data to database

Kindly seeking your help with the below code:
if( !$error ) {
move_uploaded_file($file_loc,$folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: AFP Webmaster' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
The code WORKS PERFECTLY on the following:
1. Storing the value on DB.
2. If i place a valid email like "name#gmail.com" in $to
What I want to happen now is assigning the value of the "reqEmail" from the database or the value from the textfield "remail" to $to.
So the email will go to the email who filled up the form.
i hope i made myself clear and understandable.
Thanks!
You're clearing your variables before sending the email, so PHP doesn't know where to send the email to. You need to place your "unset" functions after the PHP Mailer call. Try this:
if (!$error) {
move_uploaded_file($file_loc, $folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n".
$headers .= "MIME-Version: 1.0" . "\r\n".
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n" . 'Reply-To: AFP Webmaster' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}

Birthday reminder for all my users mysql

I want to make a birthday reminder and from here I don't know what to do.
In my MySQL users DB U have birthdate with [month/day/year] And is not working,no error, i updated my bday with this day and still no activity.
This is what I have so far:
<?php
$conn = new PDO('mysql:host=localhost;dbname=tbl', 'user', 'pass');
$today = date("m.d.y");
$sqlb = "SELECT `birthdate`, `name`, `surename` FROM `mls_users` WHERE birthdate = '$today'";
$userz = $conn->query($sqlb);
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
echo 'Todays is'.$name.' '.$surename.' birthday';
}
?>
I suppose you want to create an email notification.
$mail_content = '';
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
$mail_content .= 'Todays is'.$name.' '.$surename.' birthday. <br>';
}
// send mail to you
// mail code
if($mail_content){
$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, $mail_content, $headers);
}
If you have the users email IDs saved in your Db you can send email to them
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
//mail code
//email message
$mail_content = 'Happy birthday '.$name.' '.$surename.' !. <br>';
$to = $row['user_email']; //users email field
$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, $mail_content, $headers);
}
You can automate the script executing process using CRON Job

HTML Form/PHP and MySQL email notification

complete beginner at PHP and was wanting a little direction for a website I am creating. I want the admin of the website to receive an email with all of the form information aswell as it being stored in the database. The database is storing the information fine, just need an email notification. How is this achieved. My PHP code is:
<?php
session_start();
include('connection.php');
$product = $_POST['product'];
$productcomments = $_POST['productcomments'];
$name = $_POST['name'];
$address = $_POST['address'];
$age = $_POST['age'];
$delivery = $_POST['delivery'];
mysql_query("INSERT INTO orderform(product, productcomments, name, address, age, delivery)VALUES('$product', '$productcomments', '$name','$address', '$age', '$delivery')");
header("location: google.com");
$to = 'j_bussey#live.co.uk';
$subject = 'Order';
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysql_close($con);
?>
this is the very basic example of mail function. read more about mail() manual here.
$email = $_POST['email'];
$subject = "Email Subject";
$message = "Email Message Body";
mail($email, $subject, $message, "from: admin#yourdomain.com");
PHP has an awesome mail() function. I'm taking this from the documentation page here: http://us2.php.net/manual/en/function.mail.php
<?php
$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);
?>
So since you're already grabbing the variables, you would just edit the $message variable in the code above to look something like this:
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
etc, etc. If you don't want to assume that they have html emails enabled, you would use \n instead of <br />
Edit:
You also need to change your header('Location: google.com'); to header('Location: http://www.google.com'); or wherever you want to redirect after the email has been sent off.

Why does MySQL expect a parameter in this case

I just want to get an email from a customer table and send message to that email. I am getting this error
"Warning: mysql_num_rows() expects parameter 1 to be"
My code is as follows:
<?php
$mysql = mysql_connect("localhost", "hname", "passs", "dbname");
$getusers = mysql_query("SELECT * FROM customer");
while ($row = mysql_fetch_array($result)) {
sendMail($row['email']);
}
mysql_free_result($result);
function sendMail($to){
$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);
}
?>
You are making a myslqi connection but then using mysql_* (no i) functions. You should use all mysqli.
You can try this code :
while ($row = mysql_fetch_array($result)) {
TO
while ($row = mysql_fetch_array($getusers)) {

Categories