简体   繁体   English

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

[英]Error when sending email from localhost using php

I'm trying to send some email from localhost XAMPP to outlook mail but it only display "Error" message. 我正在尝试从本地XAMPP向Outlook邮件发送一些电子邮件,但它仅显示“错误”消息。 My code is working properly in web server side. 我的代码在Web服务器端正常工作。

I'm using 64 bit. 我正在使用64位。 I follow the instruction properly, how to set up sending email using XAMPP. 我正确地遵循了说明,如何设置使用XAMPP发送电子邮件。

Sometimes the email sent but mostly it's not and always "connection timeout". 有时发送的电子邮件,但大多数时候不是,并且总是“连接超时”。 Even if it display email "sent", i didn't receive any email. 即使显示“已发送”电子邮件,我也没有收到任何电子邮件。

How to fix this? 如何解决这个问题?

Here's the php.ini 这是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 sendmail.ini

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

My email php 我的电子邮件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";
}

Does your machine has access to port 587 ?. 您的机器是否可以访问端口587? (you can try with telnet or putty). (您可以尝试使用telnet或腻子)。 If the port is closed you should take a look at your firewall or your router. 如果端口关闭,则应查看防火墙或路由器。

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

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