Pergunta

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!"; 
?>
Foi útil?

Solução

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';

Outras dicas

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'];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top