magento发送邮件是基于服务器 sendmail服务(不作评价) 我们常常面对无法发送邮件的问题 在查出原因之前 不妨试试以下方法 这里提供的是php方法 不习惯也可以使用php-java-bridge使用java操作 因为不同的邮局结果也会不一样 建议开启调试 无误后再关闭
使用socket(套接字)发送邮件:
private $lastmessage; //记录最后返回的响应信息
private $lastact; //最后的动作,字符串形式
private $welcome; //用在HELO后面,欢迎用户
private $debug; //是否显示调试信息
private $smtp; //smtp服务器 需要与发送邮箱对应
private $port; //smtp端口号
private $fp; //socket句柄
private function init_mail($smtp, $welcome = "", $debug = false) {
if (empty($smtp))
die("SMTP cannt be NULL!");
$this->smtp = $smtp;
if (empty($welcome)) {
$this->welcome = gethostbyaddr("localhost");
}
else
$this->welcome = $welcome;
$this->debug = $debug;
$this->lastmessage = "";
$this->lastact = "";
$this->port = "25";
}
private function show_debug($body, $inout) {
if ($this->debug) {
if ($inout === "in") {
$m = '<< ';
}
else
$m = '>> ';
if (!ereg("\n$", $body))
$body .= "<br />";
$body = nl2br($body);
echo "<span style="color: #999999;">$m$body</span>";
}
}
private function do_command($command) {
$this->lastact = $command;
$this->show_debug($this->lastact, "out");
fputs($this->fp, $this->lastact);
$this->lastmessage = fgets($this->fp, 512);
$this->show_debug($this->lastmessage, "in");
if (!ereg("^[23]", $this->lastmessage)) {
return false;
}
else
return true;
}
private function sendMail($body, $email) {
$this->lastact = "connect";
$this->show_debug("Connect to SMTP server : " . $this->smtp, "out");
$this->fp = fsockopen($this->smtp, $this->port);
if ($this->fp) {
set_socket_blocking($this->fp, true);
$this->lastmessage = fgets($this->fp, 512);
$this->show_debug($this->lastmessage, "in");
if (!ereg("^220", $this->lastmessage)) {
return false;
} else {
$this->lastact = "HELO " . $this->welcome . "\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$this->lastact = "AUTH LOGIN\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$this->lastact = base64_encode("/*发送邮件的邮箱*/")."\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}$this->lastact = base64_encode("/*邮箱密码*/")."\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$this->lastact = "MAIL FROM:</*发送邮箱*/>\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$this->lastact = "RCPT TO:</*目标邮箱*/>\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$this->lastact = "DATA\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
$head = "Subject: /*主题*/\n\n";
if (!ereg($head, $body)) {
$body = $head . $body;
}
$head = "From: /*邮件中显示的邮箱 如果不是上面的邮箱 会提示代发*/\n";
if (!ereg($head, $body)) {
$body = $head . $body;
}
$head = "To: /*目的邮箱 如果不是上面的邮箱 即为密送*/\n";
if (!ereg($head, $body)) {
$body = $head . $body;
}
if (!ereg("\n\.\n", $body))
$body .= "\n.\n";
$this->show_debug($body, "out");
fputs($this->fp, $body);
$this->lastact = "QUIT\n";
if (!$this->do_command($this->lastact)) {
fclose($this->fp);
return false;
}
}
return true;
} else {
$this->show_debug("Connect failed!", "in");
return false;
}
}

