首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP :在本地主机中发送邮件

PHP :在本地主机中发送邮件
EN

Stack Overflow用户
提问于 2013-03-07 17:27:33
回答 4查看 103.5K关注 0票数 25

我想通过本地托管的php代码发送电子邮件。

代码语言:javascript
复制
<?php 
$email  = "myemail@local.com"; 
$titre   = "My subject"; 
$message = "Text message !"; 
mail($email, $titre, $message); 
?>

当我运行这段代码时,我得到以下错误:

代码语言:javascript
复制
Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\...

我查看了php.ini文件,它似乎已经配置得很好了。

代码语言:javascript
复制
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

我该如何解决这个问题呢?

谢谢

EN

回答 4

Stack Overflow用户

发布于 2013-11-03 23:55:08

我在这上面花了好几个小时。我过去不会收到错误,但邮件从来没有发送过。最后,我找到了一个解决方案,我想和大家分享一下。

代码语言:javascript
复制
<?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>';
}
?>
票数 6
EN

Stack Overflow用户

发布于 2013-03-07 17:32:06

您需要安装本地邮件服务器才能做到这一点。如果要将其发送到外部电子邮件地址,它可能会以不需要的电子邮件结束,或者根本不会到达。

我使用的一个好的邮件服务器(我在Linux上使用它,但它也可以在Windows上使用)是Axigen:http://www.axigen.com/mail-server/download/

你可能需要一些邮件服务器的经验来安装它,但是一旦它工作了,你就可以用它做任何你想做的事情。

票数 5
EN

Stack Overflow用户

发布于 2013-03-07 17:30:54

尝尝这个

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15267423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档