通过PHP发送电子邮件无法正常工

时间:2014-04-09 15:35:48

标签: php email html-email

我有一段代码无法正常工作我不明白为什么,我试图将信息发送到数据库并发送到电子邮件地址,它将它保存到数据库但是会不发送到电子邮件,即使它回应您的电子邮件已成功发送。任何帮助赞赏。

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

2 个答案:

答案 0 :(得分:0)

您使用的是错误的邮件。

这意味着像这样使用:

mail(to,subject,message,headers,parameters)

所以你的意思是:

$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);

这是假设您的$_REQUEST正在运作。

您的第二个mail()正在使用您尚未设置的变量($comments$place)等。

先设置它们:

$place = $_POST['place'];
$comments = $_POST['comments'];

答案 1 :(得分:0)

首先,如果你的localhost没有正确配置,它就不会发送你的电子邮件,所以我建议你在你的服务器上测试一下。

有些库可以帮助您获得更好的代码,例如php-simple-mail 例如:

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