【问题标题】:Send Email from Localhost XAMPP [duplicate]从 Localhost XAMPP 发送电子邮件 [重复]
【发布时间】:2016-05-13 15:53:22
【问题描述】:

我可以知道如何将电子邮件从 localhost 发送到 gmail 或其他电子邮件帐户吗?我已经对此事进行了研究并尝试这样做,但仍然无法发送电子邮件。以下是我编辑的sendmail.iniphp.ini 文件,

sendmail.ini

smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
error_logfile=error.log
debug_logfile=debug.log
auth_username=khairulamran.nazri@gmail.com
auth_password=[email password]
pop3_server=
pop3_username=
pop3_password=
force_sender=khairulamran.nazri@gmail.com
force_recipient=
hostname=smtp.gmail.com

php.ini - 邮件功能

SMTP = smtp.gmail.com
smtp_port = 465
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off

contact.php - 发送电子邮件

<?php
if(isset($_POST['submitted'])){
    $name=$_POST['name'];
    $to=$_POST['email'];
    $hp=$_POST['hp'];
    $subject="Test";
    $msg="Thank you for Register. Your Name is ".$name." and Hp no. is ".$hp;
    $header="khairulamran.nazri@gmail.com";
    $success=mail($to,$subject,$msg,$header);
    if($success==true){
        echo "Email send successfully ";
    } else{
        echo "Error sending email";
    }
}
?>
<form name="contact" method="post" action="">
Nama:<input type="text" name="name"><p>
Email:<input type="text" name="email"><p>
Hp:<input type="text" name="hp">
<input type="submit" name="submitted" value="Submit">
</form>

如果点击提交,则不会发送已提交的邮件。

【问题讨论】:

    标签: php email sendmail sendmail.exe


    【解决方案1】:

    尝试使用邮件程序库。我用过,它会完美地工作。 按照这个。 https://github.com/PHPMailer/PHPMailer

    您只需要输入您的 gmail 凭据,然后就可以使用了。

    【讨论】:

    • 想再次确认,如果我使用mailer库,是否还需要编辑php.ini和sendmail.ini文件?
    • 不,您只需在邮件库中填写信息。
    【解决方案2】:

    这是我的情况的解决方案。如果有人也需要有关此案例的信息,希望它会很有用。

    <?php
    if(isset($_POST['submitted'])){
        $name=$_POST['name'];
        $to=$_POST['email'];
        $hp=$_POST['hp'];
    
        require '../PHPMailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'myemail@gmail.com';                 // SMTP username
        $mail->Password = 'mypassword';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to
        $mail->setFrom('khairulamran.nazri@gmail.com', 'Amran');
        $mail->addAddress($to);               // Name is optional
        $mail->addReplyTo('khairulamran.nazri@gmail.com', 'Information');
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Test Email';
        $mail->Body    = 'Body of message goes here';
        $mail->AltBody = 'Body of message goes here<for non html mail client>';
        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
    }
    ?>
    
    <form name="contact" method="post" action="">
    Nama:<input type="text" name="name"><p>
    Email:<input type="text" name="email"><p>
    Hp:<input type="text" name="hp">
    <input type="submit" name="submitted" value="Submit">
    </form>
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2017-01-25
      • 2012-05-07
      • 2012-01-27
      • 1970-01-01
      • 2014-08-28
      • 2016-12-11
      • 1970-01-01
      • 2014-06-09
      相关资源
      最近更新 更多