使用php从localhost发送电子邮件时出错

时间:2014-11-10 07:31:27

标签: php email

我试图从localhost XAMPP发送一些电子邮件到Outlook邮件,但它只显示"错误"信息。我的代码在Web服务器端正常工作。

我使用的是64位。我正确地按照说明操作,如何使用XAMPP设置发送电子邮件。

有时会发送电子邮件,但大多数情况下并非总是"连接超时"。即使它显示电子邮件"已发送",我也没有收到任何电子邮件。

如何解决这个问题?

这是php.ini

 SMTP = smtp.live.com
 smtp_port = 587
 sendmail_from = same email as sendmail.ini
 sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

sendmail.ini

 smtp_server=smtp.live.com
 smtp_port=587
 auth_username=outlook email
 auth_password=********
 force_sender=outlook email

我的电子邮件php

error_reporting(E_ALL);
ini_set('display_errors', 1);

/* Email Detials */

$mail_to      = "email";
$from_mail    = "email";
$from_name    = "title";
$reply_to     = "";
$subject      = "subj...";
$message_body = "";

/* Attachment File 
Attachment location */

$file_name = "filename.xml";
$path = "C:/xampp/htdocs/Email/";

// Read the file content

$file      = $path . $file_name;
$file_size = filesize($file);
$handle    = fopen($file, "r");
$content   = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));

/* Set the email header 
Generate a boundary */

$boundary = md5(uniqid(time()));

// Email header

$header = "From: " . $from_mail . " \r\n";
$header .= "Reply-To: " . $reply_to . "\r\n";
$header .= "MIME-Version: 1.0\r\n";

// Multipart wraps the Email Content and Attachment
$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"" . $boundary . "\"";

$message_body .= "This is a multi-part message in MIME format.\r\n\r\n";
$message_body .= "--" . $boundary . "\r\n";

/* Email content
Content-type can be text/plain or text/html */

$message_body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message_body .= "Content-Transfer-Encoding: 7bit\r\n";
$message_body .= "\r\n";
$message_body .= "$message_body\r\n";
$message_body .= "--" . $boundary . "\r\n";

/* Attachment
Edit content type for different file extensions */

$message_body .= "Content-Type: application/xml;\r\n";
$message_body .= " name=\"" . $file_name . "\"\r\n";
$message_body .= "Content-Transfer-Encoding: base64\r\n";
$message_body .= "Content-Disposition: attachment;\r\n";
$message_body .= " filename=\"" . $file_name . "\"\r\n";
$message_body .= "\r\n" . $content . "\r\n";
$message_body .= "--" . $boundary . "--\r\n";

// Send email
if (mail($mail_to, $subject, $message_body, $header)) {
    echo "Sent";
} else {
    echo "Error";
}

1 个答案:

答案 0 :(得分:0)

您的机器是否可以访问端口587? (你可以试试telnet或putty)。如果端口关闭,您应该查看防火墙或路由器。