sending email through php not working - php

I have a piece of code that will not work i don't understand why, i am trying to get the information sent to the database and also sent to the email address, it is saving it to the database but will not send to the email, even though it echoes your email was successfully sent. Any help appreciated.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$to = "edonaghy--#hotmail.co.uk";
$subject = "Dress fitting";
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$headers = "From: $email";
$sent = mail($to, $date, $headers);
if($sent)
{
print "Your mail was sent successfully";
}
else
{
print "We encountered an error sending your mail";
}
mysql_select_db("lr", $con);
mail("edonaghy--#hotmail.co.uk", $time, $date, $place, $comments);
$sql="INSERT INTO fitting (time, date, place, comments) VALUES ('$_POST[time]','$_POST[date]','$_POST[place]','$_POST[comments]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Information has been sent!";
?>

First of all, it wont send your e-mail if your localhost werent properly configured, so I suggest you to test on your server if you have one.
There are libraries that can help you with a better code like php-simple-mail
Example:
require 'class.simple_mail.php';
$mailer = new SimpleMail();
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$message = "<strong>My message on date:".$date." ".$time."</strong>";
$send = $mailer->setTo('edonaghy--#hotmail.co.uk', 'Your Email')
->setSubject('Test Message')
->setFrom('no-reply#domain.com', 'Domain.com')
->addMailHeader('Reply-To', 'no-reply#domain.com', 'Domain.com')
->addMailHeader('Cc', 'bill#example.com', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Email sent successfully' : 'Could not send email';

You're using mail wrong.
It's meant to be used like this:
mail(to,subject,message,headers,parameters)
So yours would be:
$to = "email#hotmail.co.uk";
$subject = "Dress fitting";
$headers = "From: email#hotmail.co.uk";
$message = "Time:".$_REQUEST['time']."\r\n";
$message .= "Date:".$_REQUEST['date']."\r\n";
mail($to,$subject,$message,$headers);
This is assuming your $_REQUEST's are working.
Your second mail() is using variables that you haven't even set yet ($comments, $place) etc.
Set them first:
$place = $_POST['place'];
$comments = $_POST['comments'];

Related

adding data to DB and sending email after form submitted

I don't know why but I don't manage to get any email after html form is submitted.
I can see the data in the DB but I'm not receiving any Emails.
I put some code after the sending that work just fine. Only the mails are not sending.
Please help me.
The PHP file:
<?php
$Name = $_POST['Name'];
$Phone= $_POST['Phone'];
$Email= $_POST['Email'];
$Subject= $_POST['Subject'];
$Message= $_POST['message'];
$conn = mysql_connect("MyServerName", "userName", "password");
mysql_select_db("Mydbname",$conn);
$sql = "INSERT INTO FC(Name, Phone, Email, Subject, Message)
VALUES ('$Name', '$Phone', '$Email', '$Subject', '$Message')";
if (mysql_query($sql, $conn)) {
$to = "myEmail#test.com"; // this is your Email address
$from = "myEmail#test.com"; // this is the sender's Email address
$headers = "From: " . $Email;
$subject = "Form submission - " . $Subject;
$message = $Name . " wrote the following:" . "\n\n" . $Message . "\n\n" . "His Phone number is: " . $Phone;
mail($to,$subject,$message,$headers);
echo '<html>
<head>
<meta http-equiv="refresh" content="0.1;url=http://www.google.com" />
</head>
<body>';
echo '<script language="javascript">';
echo 'alert("message successfully sent! we will contact you shortly.")';
echo '</script>';
echo '</body></html>';
} else {
echo "something went wrong";
}
?>
Start by checking the return value of mail to see if the message is being accepted by your SMTP server.
Please make sure that mail() is not blocked in your php.ini file.
If your mail function returns FALSE then you would KNOW there's a problem with your mail configuration. If it returns TRUE then there could still be something wrong with your mail configuration. You would need to check the mail log in either case to see if you are getting any errors.
The kicker is that php may be handing off the mail, your server may be sending off the mail, and it may get spam filtered along the way.
You could try explicitly setting the From address using the -f parameter, as explained in example #3 in the php.net manual page for mail:
<?php
mail('nobody#example.com', 'the subject', 'the message', null,
'-fwebmaster#example.com');
?>
So for you, your From address could be invalid based on your hosting specifications but if its not:
$from = "-fmyEmail#test.com";
mail($to,$subject,$message,$headers,$from);

Mail function on Live Server is Not sending Mails

I have a website and have enabled to send mail using the By default mail function of php
My code is this
I have tested it from other posts also.. and for me it is correct.. but it is still not sending the message. Please tell me.. where is the problem
<?php
include_once './config.php';
$con=mysqli_connect(mysql_host,mysql_user,mysql_password,mysql_database);
$Roll = $_REQUEST['UserName'];
ini_set('display_errors',1);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$confirmCode = md5(uniqid(rand()));
$tbl_name1 = "temp_forgot_acc";
$orderCheck = "DELETE FROM $tbl_name1 WHERE EmailId = '$Roll'";
mysqli_query($con,$orderCheck);
$order = "INSERT INTO $tbl_name1 (EmailId,confirm_code) VALUES ('$Roll','$confirmCode')";
$result = mysqli_query($con,$order);
//if($result)
// {
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to=$Roll;
// Your subject
$subject="Your Forgot Pass link here";
// From
$header = 'From: Admin <admin#test.com>' . "\r\n";
// Your message
$message="Your Comfirmation link \r\n";
$message.="http://www.test.com/test.html?passkey=$confirmCode&Email=$Roll";
// send email
mail($to,$subject,$message,$header);
// }
echo '{"data":[';
echo "{" . '"Finish":'.'"YES"}';
echo ']}';
}
mysqli_close($con);
exit();
?>
I am able to insert it in the database... but it is not sending the maill.
Try code something like this in your application:
$from = "sender id" // sender must be valid
$subject = "subject";
$message = 'mail from'.$from.'sender';
$to = "receiver id";
// send mail
$headers = 'From: <test#test.com>' . "\n";
$headers .= "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
mail($to,$subject,$message,$headers);
and check what you have in $to value..email id must be correct.

sending email to all subscribed users, selecting from sql DB and sending, not working

I have a HTML input form where a user can subscribe to a newsletter. They enter their first name, last name and email and they get added to a table in my sql database.
I have created a second html form where the "admin" can enter a subject and a message in the body textbox and click send.
Now what is supposed to happen when they click send is, that subject/body to be sent to all the users in the sql database, but it is not working! Any help please? Code below:
<?php
$user = "example";
$password = "example";
$host = "example";
$dbase = "example";
$table = "example";
$from= 'example';//specify here the address that you want email to be sent from
$subject= $_POST['subject'];
$body= $_POST['body'];
// Connection to DBase
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
$query= "SELECT * FROM $table";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];
$msg= "Dear $firstname $lastname,\n$body";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
mysqli_close($dbc);
?>
I get the confirmation at the end "email sent to (and it will display the email)", but it does not actually send and also I get "undefined variable: to" on line 28.
(I have put example on purpose at the top)
In your code
while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];
$msg= "Dear $firstname $lastname,\n$body";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
Your email address is stored in $email but you are sending email to $to. Your mail line should be:
mail($email, $subject, $msg, 'From:' . $from);
Also, you should configure your server accordingly for mail() to work. Read more at http://ie1.php.net/manual/en/function.mail.php
change
mail($to, $subject, $msg, 'From:' . $from);
to
mail($email, $subject, $msg, 'From:' . $from);

php pear mail cant send the html formatted mail

Hi here my mail is working fine, but i dont know how to send a html formatted mail, because i am using pear mail!here i am using the code to send mail using php , first calling the mail.php and mime.php. $to mention which mail id you want to send a mail, to send a mail you need specify email id and password of email which is the sender mail.
<?php
if(isset($_POST['submit']))
{
require_once "Mail.php";//calling mail function
require_once 'Mail/mimePart.php';
$from = "you#localhost.com";
$to = "you#gmail.com";//your mail id
$CompanyName = $_POST['CompanyName'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$paddress = $_POST['paddress'];
$subject = 'MOSONS Group';
$message = 'PersonName: ' .$CompanyName. "\n";
$message .= 'Mobile: ' .$mobile. "\n";
$message .= 'Email: ' .$email. "\n";
$message .= 'Message: ' .$paddress. "\n";
$host="ssl://smtp.gmail.com";
$port="465";
$username="you#gmail.com";// your email id
$password="yourpassword"; //password
$mime= "MIME-Version: 1.0\r\n";
$type= "Content-type: text/html\r\n";
$headers = array ('From' => $from,'To' => $to ,'Subject'=>
$subject,'Mime'=> $mime,'Contenttype'=> $type);
$smtp =# Mail::factory('smtp',array ('host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail = #$smtp-> send($to,$headers,$message);
if(#PEAR::isError($mail)){
echo ("<p>" .$mail->getmessage(). "</p>");
}
else
{
echo '<script type="text/javascript">alert("Thank You for Contacting us, our team will get in touch with you soon.!"); </script>';
}
}
?>
You are setting your headers incorrectly. e.g you're doing stuff like
'Contenttype'=> 'Content-type: text/html';
It should be just
'Content-Type' => 'text/html'
PEAR::Mail is a horribly crappy package. You should switch to something better, like PHPMailer or Swiftmailer.

Email not sent or not working?

I need to ask a simple question to anyone. Any clue will be much appreaciated!
Could you please tell me any reason why a php mail() may not be sent?
I am pretty sure all details entered are correct from $to to $headers etc.
Please, Francesco
Code is:
<?php
if (array_key_exists('sendEmail', $_POST)) {
$to = 'my#email.com,'.$_POST['email'].'';
$subject = 'bla bla';
$headers = 'From: my#email.com>\r\n"
."Reply-To: $email';
$message = 'Dear '.$_POST['name']."\r\n\r\n";
$message .= 'Your Booking Number is '.$_POST['ID']."\r\n";
$message .= 'Name ' .$_POST['title']. ' ' .$_POST['firstname']. ' ' .$_POST['lastname']."\r\n";
mail($to,$subject,$message,$headers);
}
?>
Try enclosing the mail() method:
if(mail(...)) {
echo "SUCCESS";
} else {
echo "FAILURE";
}
The mail method will return false if transmission failed.

Categories