如何从php脚本和localhost发送电子邮件我已经尝试了以下代码

时间:2017-09-15 06:22:12

标签: php email

<?php
$to = "karthik.hansi@infysky.com";
$subject = "First php test script";
$message = "Hi This message is for leave application ";
$headers = "From:karthik.hansi@infysky.com";
if(mail($to, $subject, $message, $headers))
{
echo "failed";

}else{
echo "sent";
}

?> 

这是我的代码我试过这个从PHP发送邮件无法发送邮件,我必须在xampp中配置。

2 个答案:

答案 0 :(得分:0)

   $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465;
    $mail->IsHTML(true);
    $mail->Username = "yourname@gmail.com";
    $mail->Password = "your_pass";
    $mail->SetFrom("your_email");
    $mail->Subject = "your_title";
    $mail->Body = "your_body";
    $mail->AddAddress("receiver_email");
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }

您必须包含phpmailer库。您可以从此处phpmailer

下载

之后,您将转到要用作发件人的Gmail帐户,并且需要设置两步验证---&gt;关闭并允许不太安全的应用程序---&gt; ON。

本指南可以帮助您set up gmail for send emails with php

我正在使用此脚本并且工作正常,请确保设置所有更改。也许你必须在xampp中添加一些更改(我没有,但我不知道你的php.ini文件,所以记住这一点。)

答案 1 :(得分:-2)

您可以使用libmail:http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en

include "libmail.php";
$m = new Mail(); // create the mail
$m->From( "leo@isp.com" );
$m->To( "destination@somewhere.fr" );
$m->Subject( "the subject of the mail" );
$m->Body( "Hello\nThis is a test of the Mail component" );
$m->Cc( "someone@somewhere.fr");
$m->Priority(4);
//  attach a file of type image/gif to be displayed in the message if possible
$m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
$m->Send(); // send the mail
echo "Mail was sent:"
echo $m->Get(); // show the mail source