PHP: отправлять почту в localhost
Я хочу отправить электронную почту через php-код, размещенный локально.
<?php
$email = "[email protected]";
$titre = "My subject";
$message = "Text message !";
mail($email, $titre, $message);
?>
Когда я запускаю этот код, я получаю следующую ошибку:
Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\...
Я зашел в файл php.ini
и, похоже, уже настроен правильно.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
Как я могу это исправить?
Спасибо
Ответы
Ответ 1
Он настроен на использование localhost:25
для почтового сервера.
В сообщении об ошибке говорится, что он не может подключиться к localhost:25
.
Поэтому у вас есть два варианта:
- Установить/правильно настроить SMTP-сервер на локальном хосте 25
- Измените конфигурацию, чтобы указать на другой SMTP-сервер, с которым вы можете подключиться к
Ответ 2
Для этого вам потребуется установить локальный почтовый сервер.
Если вы хотите отправить его на внешние адреса электронной почты, это может привести к нежелательным сообщениям электронной почты или может не прийти вообще.
Хороший почтовый сервер, который я использую (я использую его в Linux, но он также доступен для Windows) - это Axigen:
http://www.axigen.com/mail-server/download/
Возможно, вам понадобится опыт работы с почтовыми серверами для его установки, но как только он сработает, вы можете делать с ним все, что хотите.
Ответ 3
Я потратил на это несколько часов. Раньше я не получал ошибок, но письма никогда не отправлялись. Наконец, я нашел решение, и я хотел бы поделиться им.
<?php
include 'nav.php';
/*
Download PhpMailer from the following link:
https://github.com/Synchro/PHPMailer (CLick on Download zip on the right side)
Extract the PHPMailer-master folder into your xampp->htdocs folder
Make changes in the following code and its done :-)
You will receive the mail with the name Root User.
To change the name, go to class.phpmailer.php file in your PHPMailer-master folder,
And change the name here:
public $FromName = 'Root User';
*/
require("PHPMailer-master/PHPMailerAutoload.php"); //or select the proper destination for this file if your page is in some //other folder
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465"); //No further need to edit your configuration files.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "ssl";
$mail->Username = "[email protected]"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "trials.php.php"; //this account password.
$mail->Port = "465";
$mail->isSMTP(); // telling the class to use SMTP
$rec1="[email protected]"; //receiver. email addresses to which u want to send the mail.
$mail->AddAddress($rec1);
$mail->Subject = "Eventbook";
$mail->Body = "Hello hi, testing";
$mail->WordWrap = 200;
if(!$mail->Send()) {
echo 'Message was not sent!.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo //Fill in the document.location thing
'<script type="text/javascript">
if(confirm("Your mail has been sent"))
document.location = "/";
</script>';
}
?>
Ответ 4
попробуйте это
ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: [email protected]" . "\r\n";
mail("[email protected]","test subject","test body",$headers);