簡體   English   中英

使用php從localhost發送電子郵件時出錯

[英]Error when sending email from localhost using php

我正在嘗試從本地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";
}

您的機器是否可以訪問端口587? (您可以嘗試使用telnet或膩子)。 如果端口關閉,則應查看防火牆或路由器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM