[英]PHP : send mail in localhost
我想通過本地托管的 php 代碼發送電子郵件。
<?php
$email = "myemail@local.com";
$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
我怎樣才能解決這個問題 ?
謝謝
它被配置為使用localhost:25
作為郵件服務器。
錯誤消息說它無法連接到localhost:25
。
因此,您有兩個選擇:
我花了幾個小時在這上面。 我過去沒有收到錯誤,但從未發送過郵件。 最后我找到了一個解決方案,我想分享一下。
<?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 = "trials.php@gmail.com"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "trials.php.php"; //this account's password.
$mail->Port = "465";
$mail->isSMTP(); // telling the class to use SMTP
$rec1="trials.php@gmail.com"; //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>';
}
?>
您需要安裝本地郵件服務器才能執行此操作。 如果您想將其發送到外部電子郵件地址,它可能會以不需要的電子郵件告終,或者可能根本不會到達。
我使用的一個很好的郵件服務器(我在 Linux 上使用它,但它也可用於 Windows)是 Axigen: http ://www.axigen.com/mail-server/download/
您可能需要一些郵件服務器方面的經驗來安裝它,但是一旦它工作起來,您就可以對它做任何想做的事情。
試試這個
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: test@gmail.com" . "\r\n";
mail("email@domain.com","test subject","test body",$headers);
可以在不使用任何繁重的庫的情況下發送電子郵件,我在此處包含了我的示例。
用於 PHP 的輕量級 SMTP 電子郵件發件人
https://github.com/jerryurenaa/EZMAIL
在生產和開發環境中都經過測試。
最重要的是,除非您的 IP 被服務器列入黑名單,否則電子郵件不會變成垃圾郵件。
干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.