我已经安装了 apache-5.4.2、PHP-5.4.11 和 Mysql-5.5.29 。我想使用 php 发送邮件。我意识到我的 PC 上需要有 SMTP 服务器才能发送邮件。谁能告诉我如何安装 SMTP 服务器来发送邮件的详细信息。请给我详细信息,因为我是新手。仅供引用,我在 PHP 中使用以下代码。谢谢。
<?php
$to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e8909192a88f85898184c68b8785" rel="noreferrer noopener nofollow">[email protected]</a>";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabbb8b99abdb7bbb3b6f4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
最佳答案
假设您可以访问标准电子邮件地址,那么您的电脑上不需要 SMTP 服务器,您只需在 php.ini 中设置您的详细信息即可。
如果您打开 php.ini 文件,并找到此部分;
[mail function]
SMTP = [Enter You Email SMTP address e.g. smtp.mymail.com]
smtp_port = 25
sendmail_from = [Enter your From Email Address e.g. <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b8b095b8acb8b4bcb9fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>]
auth_username = [Enter your Email Address UserName e.g. me1234]
auth_password = [Enter your Email Address Password e.g. password1234]
如果您输入为常规电子邮件地址提供的值(不带方括号!),重新启动您的 Web 服务器和 PHP,那么您应该可以启动并运行...
编辑:
似乎 GMAIL/Google Apps 需要 SSL 才能发送电子邮件。
因此,这里有一个 StackOverflow 问题.. How do I Send email using Gmail through mail() ? Where do I put the password?
完整教程在这里... http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
其中有通过 gmail 和 PHPMailer 发送电子邮件的完整教程。摘录如下:
1) 如果您没有,请注册一个 GMail 帐户或设置您的域 适用于 Google 应用程序。
2)下载最新版本的PHPMailer(我使用的是5.02版本)
3) 与您的网络托管提供商核实端口 465(TCP 输出)是否已启用 打开,如果没有请他打开该端口
4) 包含 PHPMailer 类文件:
require_once('phpmailer/class.phpmailer.php');
5) 创建这两个常量变量来存储您的 GMail 登录信息和 密码。如果您有以下情况,请使用您的 Google Apps 邮件帐户登录: 有一个。
define('GUSER', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89f0e6fcc9eee4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>'); // GMail username
define('GPWD', 'password'); // GMail password
6) 使用以下函数发送电子邮件(添加 在您包含的文件之一中运行):
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
该功能内的大部分设置都是GMail 所需要的。在搜索 PHPmailer 教程时,我发现了具有不同端口和安全设置的文章。我的建议是使用本教程中的设置。
7) 在代码中调用该函数:
smtpmailer('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72061d321f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>', '', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6204100d0f220f030b0e4c010d0f" rel="noreferrer noopener nofollow">[email protected]</a>', 'yourName', 'test mail message', 'Hello World!');
在您的应用程序中使用这种更“高级”的用法:
if (smtpmailer('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a6bd92bfb3bbbefcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cedac7c5e8c5c9c1c486cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>', 'yourName', 'test mail message', 'Hello World!')) {
// do something
}
if (!empty($error)) echo $error;
关于php - 我需要使用 PHP 发送电子邮件,如何在我的 PC(Windows XP)上安装 smtp 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14629153/