【问题标题】:Sending email in Delphi without smtp and using php function at server在没有 smtp 的情况下在 Delphi 中发送电子邮件并在服务器上使用 php 函数
【发布时间】:2009-05-17 16:46:54
【问题描述】:

使用Delphi,我想使用winsock向我的网络服务器发送一条文本消息,然后使用服务器上的电子邮件php函数发布消息。

首先我完成了发送过程(Procedure SendEmail):它读取一个文本文件(日志)并将其发布到我的服务器。在服务器上,消息由名为 email.php 的电子邮件 php 函数接收(请参阅下面该函数的内容):

Procedure SendEmail;

var
WSADat:WSAData; // winsock.pas
Texto:TextFile;
Client:TSocket;
Info,Posting,Linha,Content:String;
SockAddrIn:SockAddr_In; // winsock.pas
begin
try
if not FileExists(Log) then exit;
AssignFile(Texto, Log); // text to be sent 
Reset(Texto);
while not Eof(Texto) do
begin
ReadLn(Texto, Linha);
Content:=Content+#13#10+Linha;
end;
CloseFile(Texto);
DeleteFile(PChar(Log));
WSAStartUp(257,WSADat);
Client:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
SockAddrIn.sin_family:=AF_INET;
SockAddrIn.sin_port:=htons(80);
SockAddrIn.sin_addr.S_addr:=inet_addr('60.64.10.42'); // myserver IP
if Connect(Client,SockAddrIn,SizeOf(SockAddrIn))=0 then begin
Info:='Sender='+CFG.Email+'&content='+Content;
Posting:='POST /blogwp/email.php HTTP/1.0' #13#10
'Connection: close' #13#10
'Content-Type: application/x-www-form-urlencoded' #13#10
'Content-Length: '+IntToStr(Length(Info)) #13#10
'Host: http://myserver.com' #13#10
'Accept: text/html' +#13#10+#13#10+
Info+#13#10;
Send(Client,Pointer(Posting)^,Length(Posting),0);
end;
CloseSocket(Client);
except
exit;
end;
end;
[... some  mutex test...]
end.

服务端 email.php 函数说明:

<?php
$to =$_POST["sender"];
$subject ="mymessages";
$message =$_POST["content"];
$from ="From: some at somedomain dot com";

$headers =implode("\n",array("From: $headers","Subject: $subject","Return-Path: $headers","MIME-Version: 1.0?","X-Priority: 3","Content-Type: text/html" ));

$flag = mail($to,$subject,$message,$from); // create variables

if($flag)
{
echo "ok!";
}
else
{
echo "no ok =/";
}
?>

(注意:我已经按照php邮件功能的这个指南:http://us3.php.net/manual/en/book.mail.php

这里的总体思路是不使用 smtp 将消息发送到服务器。但是,据我所见,邮件似乎被丢弃在我的服务器上。我对这个过程很有信心,但不确定 php 邮件功能。这东西不容易调试。知道出了什么问题吗?或任何其他看起来相似的解决方案?

任何帮助将不胜感激。谢谢。

[编辑] 我还在我的服务器站点上询问了有关使用 smtp 的支持。如果没有先进行检查,我似乎无法发送任何邮件(使用 smtp):

We use pop-before-smtp mail authentication. You first need to

在发送之前检查邮件 任何。你也没有设置 smtp 在您的电子邮件客户端中进行身份验证 设置。一旦你检查了 带有 POP 身份验证的邮件不是 需要。

We use POP before SMTP authentication as it is a fairly

防止垃圾邮件发送者和 打开继电器。不幸的是,这是 我们选择的配置。 对于希望发送邮件的用户 列表,我们有 Mailman ValueApp, 但对于标准电子邮件,POP 之前 SMTP 是服务器的设置方式。

【问题讨论】:

  • 为什么不在服务器中使用网络服务? Web 服务将收集消息数据并发送消息。它更简单,而且不是面向套接字的。

标签: php delphi email


【解决方案1】:

下面这行是错误的:

$headers =implode("\n",array("From: $headers","Subject: $subject","Return-Path: $headers","MIME-Version: 1.0?","X-Priority: 3","Content-Type: text/html" ));

应该是

$headers =implode("\r\n", array("From: $from","Return-Path: $from","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html"));

虽然您的邮件服务器可能已损坏并需要“\n”。

$from 也只能是电子邮件地址,不能包含“发件人:”。

无需设置主题标头 - PHP 会为您完成。

【讨论】:

    【解决方案2】:

    您需要确保 POST 值名称的大小写匹配 - 您有

    $to =$_POST["sender"];
    

    Info:='Sender='+CFG.Email
    

    【讨论】:

      【解决方案3】:

      由于您注意到 ISP 需要通过 POP-before-SMTP 进行身份验证,因此您应该考虑使用类似 PHP Mailer 的东西来促进这种交互 - 或者使用POP 的 PHP 函数(这恰好与 IMAP 和 NNTP 的完全相同)。

      【讨论】:

      • 谢谢 - 我会看看 PHO Mailer。否则,什么是正确的流行电子邮件检查器?这可以吗 POP :
      • 从 php.net/imap-open 可以看到第二个例子:$mbox = imap_open("{localhost:110/pop3}INBOX", "user_id", "password");
      • 谢谢。我会试一试的。
      【解决方案4】:

      在 PHP 端,您可以使用 fsockopen()、fputs()、fgets() 和 fclose() 来处理与 POP 服务器的连接需求。 fsockopen() 的 PHP 帮助应该为您提供所需的详细信息。

      旁注:也许不是问题,但您需要确保您没有创建开放中继的混合体(让其他人使用您的 email.php 端点代表您发送电子邮件)。

      【讨论】:

      • 谢谢,我会看看并尝试 fsockopen()
      【解决方案5】:

      测试您的 PHP 代码

      为什么不尝试写入文件? 我会用日期/时间和日志文件内容写入服务器上的文件,然后从http://myserver.com/log.txt访问它

      Delphi部分:

      我不会直接使用套接字,你为什么不使用像 Indy (TIDHTTP) 这样的库。

      使用起来会简单得多。

      POST 类似于 IdHttp1.Post(...)

      【讨论】:

        【解决方案6】:

        好的,谢谢...我已经应用了更正。现在我有:

        Procedure EnviarEmail;
        var
          WSADat:WSAData;
          Texto:TextFile;
          Client:TSocket;
          Info,Posting,Linha,Content:String;
          SockAddrIn:SockAddr_In;
        begin
          try
            if not FileExists(Log) then exit;
            AssignFile(Texto, Log);
            Reset(Texto);
            while not Eof(Texto) do
            begin
              ReadLn(Texto, Linha);
              Content:=Content+#13#10+Linha;
            end;
            CloseFile(Texto);
            DeleteFile(PChar(Log));
            WSAStartUp(257,WSADat);
            Client:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
            SockAddrIn.sin_family:=AF_INET;
            SockAddrIn.sin_port:=htons(80);
            SockAddrIn.sin_addr.S_addr:=inet_addr('86.88.10.42');
            if Connect(Client,SockAddrIn,SizeOf(SockAddrIn))=0 then begin
              Info:='Sender='+CFG.Email+'&Content='+Content;
              Posting:='POST /blogwp/email.php HTTP/1.0'               +#13#10+
                     'Connection: close'                               +#13#10+
                     'Content-Type: application/x-www-form-urlencoded' +#13#10+
                     'Content-Length: '+IntToStr(Length(Info))         +#13#10+
                     'Host: http://somedomain.com'                     +#13#10+
                     'Accept: text/html'                               +#13#10+#13#10+
                     Info+#13#10;
              Send(Client,Pointer(Posting)^,Length(Posting),0);
            end;
            CloseSocket(Client);
           // ShowMessage('INFO == ' +Info + ' POSTING == '+ Posting);
          except
            exit;
          end;
        end;
        [... some mutex check...]
        

        服务器上的 email.php 是:

        <?php
        $to =$_POST["Sender"];
        $subject ="mymessages";
        $message =$_POST["content"];
        $from ="From: some at somedomain dot com";
        
        $headers =implode("\r\n", array("From: $from","Return-Path: $from","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html"));
        
        $flag = mail($to,$subject,$message,$from); // create variables
        
        if($flag)
        {
        echo "ok!";
        }
        else
        {
        echo "no ok =/";
        }
        ?>
        

        仍然没有收到邮件。程序中的 ShowMessage 向我显示 info 变量的类似内容:

        Sender=validEmailAddressContent=<..... data ....> 
        

        这可能是错误的 - 地址和内容之间至少缺少一个空格或换行符 (?)。这个“信息”变量应该如何格式化?

        谢谢。

        【讨论】:

        • 请评论您的帖子,而不是“回答”它们。
        • 我已经设法使用 PEAR 和这个脚本发送邮件: "; $to = "姓名"; $subject = "嗨!"; $body = "嗨"; $host = "smtp 服务器"; $用户名 = "用户"; $密码=“通过”; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'user' => $username, 'pass' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("

          " . $mail->getMessage() . "

          "); } else { echo("

          消息已发送!

          "); } ?>
        猜你喜欢
        • 2011-01-21
        • 2014-08-11
        • 2014-01-13
        • 2013-07-22
        • 1970-01-01
        • 2011-06-06
        • 2011-03-07
        • 2011-06-25
        • 2014-10-06
        相关资源
        最近更新 更多