在本地主机中使用php发送电子邮件
我想用php使用xampp发送电子邮件在本地主机中使用php发送电子邮件,php,email,xampp,Php,Email,Xampp,我想用php使用xampp发送电子邮件 <html> <head> </head> <body> <?php error_reporting(E_ALL ^ E_NOTICE); $to = $_POST['email_add']; //define the subject of the email $subject = $_POST['sbjct']; //define
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
<form action="send_email.php" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" value="Send"/>
</form>
</body>
</html>
电子邮件添加
主题
它不起作用了。我的代码有问题吗
我在云上找到了一些教程,但它提到了SMTP,我不理解它。PHP实际上并没有发送电子邮件。。。在Linux中,它将转发给sendmail或类似的守护进程;在Windows中,它将转发给SMTP服务器。根据您的操作系统,您必须设置这些其他进程,并向PHP解释如何/在何处联系它们(在
[邮件功能]
部分的PHP.ini中)。如果您在本地开发环境的Windows上使用mail()函数,您可能需要指定一个可以为您中继邮件的SMTP服务器。Linux有这个功能,Windows可能没有。您的ISP可能会向您提供此服务,或者网络邮件提供商可能会允许您通过他们转发邮件
查看php.ini文件,找到[mail]部分并填写:
SMTP=SMTP.yourprovider.com
SMTP_端口=25
从PHP邮件主页
mail()的Windows实现在许多方面与Unix实现不同。首先,它不使用本地二进制来编写消息,而只在直接套接字上运行,这意味着需要MTA监听网络套接字(可以在本地主机或远程计算机上)我想你在找什么?PHP需要一个SMTP服务器来发送电子邮件 您可以在php配置文件中指定它(例如,将其设置为ISP配置文件),或者使用“测试邮件服务器工具”(Test Mail Server Tool)捕获smtp本地呼叫并将其保存在硬盘目录中,以便调试所有内容 显然,只有当您需要调试它时,此解决方案才有效。 如果您需要发送邮件,请参阅php文档并使用ISP SMTP服务器。
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" name="submit" value="Send"/>
</form>
</body>
</html>
<?php
If(isset('submit'))
{
error_reporting(E_ALL);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$mail_sent = mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
}
?>
这是一个我在我的很多项目中重复使用的脚本,非常简单,只需复制并更改明显的值
$email_to = "myemail@gmail.com,anotheremail@gmail.com";
$email_subject = "This is the subject line";
$break = 'echo "/n"';
$form_email = "no-reply@myemail.com";
//Function to convert /n to line breaks in HTML
function nl2br2($email_message) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $email_message;
}
//Unformatted email body
$email_message = "This is the main blody of the email";
//Format string against function
nl2br2($email_message);
// create email headers
$headers = 'From: '.$form_email."\r\n".
'Reply-To: '.$form_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
$email\u to=”myemail@gmail.com,anotheremail@gmail.com";
$email\u subject=“这是主题行”;
$break='echo”/n';
$form\u email=“否-reply@myemail.com";
//函数将/n转换为HTML中的换行符
功能nl2br2($email_message){
$string=str\u replace(数组(“\r\n”、“\r”、“\n”)、“
”、$string);
返回$email_消息;
}
//无格式电子邮件正文
$email\u message=“这是电子邮件的主要部分”;
//针对函数设置字符串格式
nl2br2(电子邮件);
//创建电子邮件标题
$headers='From:'。$form\u电子邮件。“\r\n”。
“回复:”.$form\u电子邮件。“\r\n”。
“X-Mailer:PHP/”。phpversion();
@邮件($email\u to、$email\u subject、$email\u message、$headers);
Changeerror\u报告(E\u ALL^E\u通知)代码>至错误报告(E_ALL)
并从@mail()
中删除@
。您可能需要设置经过身份验证的SMTP。您可以在xammp.+1中使用mercury mail server。我在Win/XP上从未遇到过XAMPP问题,但当我切换到Windows7时,它缺少一个本地smtp服务器。安装了测试smtp服务器,一切正常。