我想通过本地托管的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
我该如何解决这个问题?
谢谢
答案 0 :(得分:15)
它被配置为使用localhost:25
作为邮件服务器。
错误消息表明它无法连接到localhost:25
。
因此,您有两种选择:
答案 1 :(得分:5)
您需要安装本地邮件服务器才能执行此操作。 如果您想将其发送到外部电子邮件地址,它可能会收到不需要的电子邮件,或者可能根本不会发送。
我使用的一个好的邮件服务器(我在Linux上使用它,但它也适用于Windows)是Axigen: http://www.axigen.com/mail-server/download/
您可能需要一些邮件服务器的经验才能安装它,但一旦工作,您可以随心所欲地做任何事情。
答案 2 :(得分:5)
我花了好几个小时。我曾经没有收到错误,但邮件从未发送过。最后我找到了一个解决方案,我想分享一下。
<?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>';
}
?>
答案 3 :(得分:1)
试试这个
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);
答案 4 :(得分:0)
可以在不使用我在此处包括示例的繁重库的情况下发送电子邮件。
用于PHP的轻量级SMTP电子邮件发件人
https://github.com/jerryurenaa/EZMAIL
在生产和开发环境中均经过测试。
最重要的是,除非您的IP被服务器列入黑名单,否则电子邮件将不会成为垃圾邮件。
欢呼。