简体   繁体   English

@mail没有用PHP发送邮件

[英]@mail not sending mail in php

This is the first time I am using the mail function. 这是我第一次使用邮件功能。 I tried to send a mail using this and the code tells me it's "successfully sent", but I didn't receive any mail. 我尝试使用此方法发送邮件,并且代码告诉我它“已成功发送”,但是我没有收到任何邮件。 I'm confused reading lots of articles on this. 我很困惑阅读很多有关此的文章。 Please help me. 请帮我。

<?php
  if(isset($_POST['author'])&& isset($_POST['subject'])&& isset($_POST['text'])) {
    //Email
    $email_to = "lucy@yahoo.com";
    $email_subject = "Query From HYPERMARKETS";
    $name = $_POST['author']; // required
    $sub = $_POST['subject']; // required
    $comments = $_POST['text']; // required

    echo "POST:".$name.$sub.$comments;
    $error_message = "";

    $string_exp = "^[a-z .'-]+$";
    if(!strcmp($string_exp,$name)) {
      $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }

    if(strlen($comments) < 5) {
      $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
      echo ("<center><table border=1 cellspacing=5 cellpadding=5><tr><th>$error_message</th></tr></table></center>");
    }
    $email_message = "Form details below.\n\n";
    $email_message .= "Name: ".$name."\n";
    $email_message .= "Subject: ".$sub."\n";
    $email_message .= "Comments: ".$comments."\n";


    // create email headers
    $headers = "From: mary@yahoo.com \r\n Reply-To: mary@yahoo.com \r\n" ;
    if(@mail($email_to, $email_subject, $email_message, $headers))
      echo "<center><table border=1 cellspacing=5 cellpadding=5><tr><th>MESSAGE SENT SUCCESSFULLY</th></tr></table></center>";
    else
      echo "<center><table border=1 cellspacing=5 cellpadding=5><tr><th>MESSAGE NOT SENT</th></tr></table></center>";
  }
  else
    echo ("<center><table border=1 cellspacing=5 cellpadding=5><tr><th>FILL ALL THE FIELDS..!!</th></tr></table></center>");
?>

Hey I have configured my php.ini file as 嘿,我已经将php.ini文件配置为

[mail function]

   sendmail_from = postmaster@localhost
   SMTP = localhost
   smtp_port = 25
   sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
   sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

It still doesnt work. 它仍然不起作用。

Disclosure: I'm one of the developers behind AlphaMail 披露:我是AlphaMail背后的开发人员之一

Since you have no error to go on, it's not that easy to say what the issue is here. 由于没有错误要继续,要说出问题所在并不容易。 I would therefore recommend that to manually try and connect to the SMTP-server from your server and send the email manually . 因此,我建议您手动尝试从您的服务器连接到SMTP服务器并手动发送电子邮件 This way you'll have the error in hand instead of second-guessing. 这样一来,您就可以掌握错误,而不必再三思而后行了。

If you don't want to do this, and just want it to "work" I would recommend that you use a Transactional Email Service such as: 如果您不希望这样做,而只是希望它“起作用”,我建议您使用事务性电子邮件服务,例如:

Why? 为什么?

  • You don't have to think that much about email delivery. 您不必对电子邮件传递考虑太多。
  • Statistics. 统计。 Let's you track Total Sent/Clicks/Opens/Bounces. 让我们跟踪“发送总数/点击次数/打开次数/跳动次数”。
  • Often web service-based instead of SMTP. 通常基于Web服务而不是SMTP。 Ie easier to handle. 即更容易处理。
  • Cleaner code (at least if you use AlphaMail that separates data from presentation). 简洁的代码(至少在使用AlphaMail将数据与表示分离的情况下)。
  • Scalable and future proof. 可扩展且面向未来。

If you choose to go with AlphaMail you could use the AlphaMail PHP-client . 如果您选择使用AlphaMail,则可以使用AlphaMail PHP-client

Example: 例:

include_once("comfirm.alphamail.client/emailservice.class.php");

$email_service = AlphaMailEmailService::create()
    ->setServiceUrl("http://api.amail.io/v1")
    ->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");

$person = new stdClass();
$person->userId = "1234";
$person->firstName = "John";
$person->lastName = "Doe";
$person->dateOfBirth = 1975;

$response = $email_service->queue(EmailMessagePayload::create()
    ->setProjectId(12345) // Your AlphaMail project (determines template, options, etc)
    ->setSender(new EmailContact("Sender Company Name", "from@example.com"))
    ->setReceiver(new EmailContact("Joe Doe", "to@example.org"))
    ->setBodyObject($person) // Any serializable object
);

Another advantage with AlphaMail is that you can edit your templates directly in the AlphaMail Dashboard , and you can format your emails using the Comlang template language . AlphaMail的另一个优点是,您可以直接在AlphaMail仪表板中编辑模板,并且可以使用Comlang模板语言来格式化电子邮件。

<html>
    <body>
        <b>Name:</b> <# payload.firstName " " payload.lastName #><br>
        <b>Date of Birth:</b> <# payload.dateOfBirth #><br>

        <# if (payload.userId != null) { #>
            <a href="/sign-up">Sign Up Free!</a>
        <# } else { #>
            <a href="/login?id=<# payload.userId #>">Sign In</a>
        <# } #>
    </body>
</html>

mail() doesn't actually send mail. mail()实际上并不发送邮件。 It merely submits the message you've generated to the host system's mail subsystem to handle the sending. 它仅将您生成的消息提交到主机系统的邮件子系统以处理发送。 A result of true indicates that the mail sending subsystem accepted the message as valid. 结果为true表示邮件发送子系统接受了该消息为有效消息。 It doesn't indicate that the message was sent. 这并不表示邮件已发送。

You'll need to look at your mail spool to see if the messages are in there awaiting delivery, and at your system's mail log to see if it is generating errors when it tries to send your PHP-generated messages. 您需要查看您的邮件后台处理程序,以查看是否有等待发送的消息在其中,以及系统的邮件日志,以查看在尝试发送PHP生成的消息时是否正在产生错误。

Even if the message has been successfully sent, it doesn't mean it will be received. 即使消息已成功发送,也并不意味着它将被接收。 It may be caught by a spam filter, or rejected by a server, or just plain sent to the wrong address. 它可能被垃圾邮件过滤器捕获,或者被服务器拒绝,或者只是被发送到错误的地址。 It may have been delivered and ended up being marked as junk mail by the receiver's e-mail client. 它可能已经送达,并最终被收件人的电子邮件客户端标记为垃圾邮件。 Any number of things can prevent a message being sent, a true response from mail is no indication that it was sent successfully. 任何事情都可以阻止邮件的发送,来自邮件的真实响应并不表示邮件已成功发送。

did you ever send mails on this server? 您曾经在该服务器上发送过邮件吗? if not: 如果不:

you have to configure mail function section in php.ini. 您必须在php.ini中配置mail function部分。

First of all you have to have SMTP support for send&receive e-mails. 首先,您必须对发送和接收电子邮件具有SMTP支持。 Second, you have to configure SMTP and smtp_port values in php.ini file. 其次,您必须在php.ini文件中配置SMTPsmtp_port值。

Some hosting requires the extra parameter for mail. 一些主机需要额外的邮件参数。 It could possibly be that. 可能就是那样。

    $ffrom = '-fmary@yahoo.com';
    if(@mail($email_to, $email_subject, $email_message, $headers, $ffrom));

It's not very common, but I have had to deal with it, and it took me ages to diagnose, so thought I would share it just in case. 它不是很常见,但是我不得不处理它,并且花了我很多年的时间才能诊断出来,所以以为我以防万一。

First, I would make sure that port 25 is open. 首先,我要确保端口25是打开的。 Determine the public IP address of the server by connecting to WhatsMyIP.org (from the same internet connection that the server uses) then try the following from the command line: 通过连接到WhatsMyIP.org (通过服务器使用的同一Internet连接)来确定服务器的公共IP地址,然后从命令行尝试以下操作:

telnet xxx.xxx.xxx.xxx 25

(replace xxx.xxx.xxx.xxx with the IP from WhatsMyIP.org) (用WhatsMyIP.org的IP替换xxx.xxx.xxx.xxx)

If you can connect, then this answer isn't for you. 如果可以连接,则此答案不适合您。 Sorry. 抱歉。

If not, check all of your local firewall settings to make sure that port 25 is open on your end. 如果不是,请检查所有本地防火墙设置,以确保端口25在您的一端打开。 Port 25 may also be blocked by the ISP. 端口25也可能被ISP阻止。 It turns out that some (maybe most) ISPs block port 25 by default to prevent spam. 事实证明,某些(也许是大多数)ISP默认情况下会阻止端口25,以防止垃圾邮件。

Misconfigured email servers are easily compromised and used in spam botnets. 错误配置的电子邮件服务器很容易受到攻击,并在垃圾邮件僵尸网络中使用。 I had to call Comcast and jump through hoops to get them to unblock port 25 before I could get my home email server working. 我必须打电话给Comcast并跳一圈,才能让它们解除25端口的阻塞,然后才能使家庭电子邮件服务器正常工作。 ( Comcast forum post about port 25 ). Comcast论坛有关端口25的帖子 )。

If that doesn't get you going, I'd take another look at the email server configuration. 如果那不能解决问题,我将再次查看电子邮件服务器的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM