简体   繁体   中英

how to send mail to wamp server?

<?php
$to       = 'mass.mari06@gmail.com';
$subject  = 'Testing sendmail.exe';
$message  = 'Hi, you just received an email using sendmail!';
$headers  = 'From: sender@gmail.com' . "\r\n" .
            'Reply-To: sender@gmail.com' . "\r\n" .
            'MIME-Version: 1.0' . "\r\n" .
            'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers))
    echo "Email sent";
else
    echo "Email sending failed";
?>

This is my coding ..but output is message send failed.. so what can i do???

You need to configure SMTP on localhost for sending email

Go to your php.ini and set below settings

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
 SMTP = mail.host.com 
 smtp_port = 25

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

For more :- WAMP send Mail using SMTP localhost

The right method of sending mail in php is via pear mail extension . PEAR_MAIL

Then you can follow the steps : 1)Install pear : click on pear.phar or pear.bet in php folder

2)set REG.ENV, if it doesn work , change environment variable : PHP_PEAR_PHP_BIN to %pathWherePhpIsInstalled/php.exe

3)install mail package

4)install net_smtp package

Sample Mail Script :

$from = 'senderemailaddress';
$to = 'mass.mari06@gmail.com';
$subject = 'Hi,Its subject!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => '465',
    'auth' => true,
    'username' => 'sender_email_addreess',
    'password' => 'pass'
));

$mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}
?>

There are two ways of sending emails. SMTP or php mail function.According to your code, you are using the php mail function.

Difference:

SMTP uses other vendor's Mail Transport Agent (MTA).

php mail function uses your own Mail Transport Agent (MTA).

Mail Transport Agent (MTA) is one kind of software, for instance, sendmail in Linux. It is not easy to setup a MTA on windows. If you just need to debug the output content of the mail, I suggest you use this tool, Test Mail Server Tool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM