PHP:在本地主机发送邮件
我想通过本地托pipe的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
文件,似乎已经configuration好了。
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25
我该如何解决这个问题?
谢谢
它被configuration为使用localhost:25
作为邮件服务器。
该错误消息说,它不能连接到localhost:25
。
所以你有两个select:
- 安装/在本地主机端口25上正确configurationSMTP服务器
- 将configuration更改为指向您可以连接到的某个其他SMTP服务器
您将需要安装本地邮件服务器才能执行此操作。 如果您想将其发送到外部电子邮件地址,则可能会以不需要的电子邮件forms发送,或者根本无法完成。
我使用的一个好的邮件服务器(我在Linux上使用它,但它也可用于Windows)是Axigen: http ://www.axigen.com/mail-server/download/
您可能需要一些邮件服务器的安装经验,但是一旦它运行,您就可以随心所欲地做任何事情。
我花了几个小时。 我曾经没有得到错误,但邮件从未被发送。 最后,我find了一个解决scheme,我想分享一下。
<?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>'; } ?>
尝试这个
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);