我需要使用PHP发送电子邮件,如何在我的PC(Windows XP)上安装smtp服务器?

I have installed apache-5.4.2, PHP-5.4.11 and Mysql-5.5.29 . I want to send mail using php. I have realized that I need a SMTP server on my PC in order to send mails. Can anyone please tell me the details on how to install a SMTP server to send mail. Please give me details on it as I am new to this. Just FYI I am using the below code in PHP. Thanks.

<?php
$to = "xyz@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "abc@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

Assuming you have access to a standard email address, then you don't need an SMTP server on your pc, you simply need to setup your details in the php.ini.

If you open your php.ini file, and find this section;

[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. me@mymail.com]

auth_username = [Enter your Email Address UserName e.g. me1234] 
auth_password = [Enter your Email Address Password e.g. password1234]

If you enter in the values supplied for your regular email address (without the square brackets!), Restart your WebServer and PHP, then you should be up and running...

EDIT:

Seems as though GMAIL / Google Apps requires SSL to send email.

As such, there's a StackOverflow Question here.. How do I Send email using Gmail through mail() ? Where do I put the password?

A full tutorial is here... http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

That has a full tutorial for sending email via gmail and PHPMailer. An excerpt from this is;


1) If you don’t have one, register a GMail account or setup your domain for Google applications.

2) Download a recent version of PHPMailer (I’m using the version 5.02)

3) Check with your web hosting provider that port 465 (TCP out) is open, if not ask him to open that port

4) Include the PHPMailer class file:

require_once('phpmailer/class.phpmailer.php');

5) Create those two constant variables to store your GMail login and password. Use the login for your Google Apps mail account if you have one.

define('GUSER', 'you@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password

6) Use the following function to send the e-mail messages (add the function in one of your included files):

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;
    }
}

Most of the settings inside the function are required by GMail. While searching for PHPmailer tutorials I found articles with different settings for the port and security. My advice is to use the settings from this tutorial.

7) Call the function within your code:

smtpmailer('to@mail.com', '', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!');

Use this more “advanced” usage inside your application:

if (smtpmailer('to@mail.com', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!')) {
    // do something
}
if (!empty($error)) echo $error;

On XP, some years ago, I used to install Argosoft Mini MailServer.

I preferred it to gmail or external ones because I can see SMTP logs. Just define your smtp as localhost in your PHP code.

You need to configure php.ini file to send email via php send mail function.

  1. Open php.ini file.

Default php.ini file settings

[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25

; For Win32 only. ; http://php.net/sendmail-from sendmail_from = you@yourdomain

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path ;sendmail_path =

these entries are required to send the mail to any mail client from mail server.

2.The following two lines don't exist:

auth_username auth_password

you need to add them to send mail from a server that requires authentication.

  1. change SMTP settings

smtp_server = mail.example.com ///your SMTP server name. smtp_port = 26 // SMTP port number auth_username = username@example.com auth_password = password sendmail_from = you@example.com // from email address.

4.SAVE php.ini file.

  1. Restart Server.

  2. DONE.