dougua4836 2015-10-03 12:33
浏览 35
已采纳

PHP联系表,不接收电子邮件[重复]

This question already has an answer here:

So, here we go with silly question number too many to count!

I've made a very simple PHP contact form using tutorials from the internet (I still need to add security measures to it but I wanted to get it working first) When I click on the send button on my website I do get the message sent script however no email arrives in my in box.

Any ideas what I'm doing wrong? The website is currently hosted locally via XAMPP.

$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$from = 'From: me@mywebsite.co.uk';
$to = 'me@mywebsite.co.uk';
$subject = 'Enquiry';

$body = "From: $name
 Company: $company
 Email: $email
 Telephone: $tel
 Message: $message
";

if ($_POST['send']) {
    if(mail($to, $subject, $body, $from)) {
        echo '<p> Your message has been sent!</p>';
    } else {
        echo '<p>Message could not be sent.<br>Please check that you have completed the name, email and message fields and try again</p>';
    }
}
</div>
  • 写回答

2条回答 默认 最新

  • dsymx68408 2015-10-03 18:06
    关注

    As when I've previously set up contact forms I've been doing so using Code Igniter I didn't realise that I couldn't use mail() without installing a mail server.

    Thanks to Parris Varney and RiggsFolly for pointing this out and thanks again to Riggs for letting me know that Code Igniter uses the PHPMailer library.

    By using PHPmailer I was able to correct the code and get the form working perfectly in very short order.

    For anyone interested the new code used with the latest version of PHPmailer is:

    $name = $_REQUEST['name'];
    $co = $_REQUEST['company'];
    $email = $_REQUEST['email'];
    $tel = $_REQUEST['tel'];
    $message = $_REQUEST['message'];
    
    require("PHPMailerAutoLoad.php");
    
    $mail = new PHPMailer();
    
    $mail->isSMTP();
    
    $mail->Host = "mail.mydomain.co.uk";
    $mail->SMTPAuth = true;
    $mail->Username = "me@mydomain.co.uk";
    $mail->Password = "password";
    $mail->SMTPAutoTLS = false;
    
    $mail->From = $email;
    $mail->addAddress("me@mydomain.co.uk", "Me");
    $mail->WordWrap = 50;
    $mail->isHTML(true);
    $mail->Subject = "Enquiry";
    
    $mail->Body = "From: $name<br>Company: $co<br>Email: $email<br>Telephone: $tel<br>Message: $message";
    $mail->AltBody = "From: $name Company: $co Email: $email Telephone: $tel Message: $message";
    
    if(!$mail->Send())
    {
        echo "Message could not be sent";
    }
    echo "Message has been sent";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教