简体   繁体   中英

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. My code is working properly in web server side.

I'm using 64 bit. I follow the instruction properly, how to set up sending email using 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

 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

My 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";
}

Does your machine has access to port 587 ?. (you can try with telnet or putty). If the port is closed you should take a look at your firewall or your router.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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