duangai9678 2014-04-16 08:15
浏览 22
已采纳

PHP电子邮件不会发送到我的电子邮件

I have some problems with sending and receiving emails with my code..

When I try this code on my localhost(XAMPP) it says: Email send, but when i look in my mail i didn't receive the mail.. can someone help? My code:

<?php

// $EmailFrom = "ME";
$EmailTo = "My_amail_adress";
$Subject = "My_subject";
$Name = stripslashes($_POST['Name']); 
$Email = stripslashes($_POST['Email']); 
$Message = stripslashes($_POST['Message']); 

//setup the email
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "
";

$success = mail($EmailTo, $Subject, $Body); //"From: <$EmailFrom>"

if ($success) {
    echo "Email send";
}
else{
    echo "Error, please try again";
}

?>
  • 写回答

2条回答 默认 最新

  • doubei2340 2014-04-16 08:25
    关注

    Do you have SMTP server configured in your localhost ?

    You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.

    for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

    in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

    in php.ini file find [mail function] and change

    SMTP=smtp.gmail.com
    smtp_port=587
    sendmail_from = my-gmail-id@gmail.com
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    

    Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

    [sendmail]
    smtp_server=smtp.gmail.com
    smtp_port=587
    error_logfile=error.log
    debug_logfile=debug.log
    auth_username=my-gmail-id@gmail.com
    auth_password=my-gmail-password
    force_sender=my-gmail-id@gmail.com
    

    Now you have done!! create php file with mail function and send mail from localhost.

    PS: don't forgot to replace my-gmail-id and my-gmail-password in above code. Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?