dqrdlqpo775594 2019-08-14 10:21
浏览 100

如何使用PHP与XAMPP从localhost发送电子邮件?

I want to send Gmail mail using PHP with Xampp from localhost. I try to see the tutorial via Youtube. But it is still not working even I follow step by step. My PHP code is as follow:

<?php
mail('mypersonalemail@gmail.com','Testing send email using PHP','Content 
here','From: otheremail@gmail.com');
?>

I also did some Xampp configuration in my "php.ini" file and "sendmail.ini" as shown below:

1) php.ini:

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

    ; For Win32 only.
    ; http://php.net/sendmail-from
    sendmail_from = mypersonalemail@gmail.com
    sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"

2) sendmail.ini:

    smtp_server=smtp.gmail.com
    smtp_port=587
    auth_username=mypersonalemail@gmail.com
    auth_password=mygmailpassword

And my file location of the PHP file is C:\xampp\htdocs\testemail.php

I run the localhost with http://localhost/testemail.php. I also have tried change the port number to 25, 587 and 465. Guide me if I missing something.

  • 写回答

2条回答 默认 最新

  • douyan1453 2019-08-14 12:34
    关注

    HTML

    <!DOCTYPE html>
    <html>
    <head>
    <title>FeedBack Form With Email Functionality</title>
    <link href="css/elements.css" rel="stylesheet">
    </head>
    <!-- Body Starts Here -->
    <body>
    <div class="container">
    <!-- Feedback Form Starts Here -->
    <div id="feedback">
    <!-- Heading Of The Form -->
    <div class="head">
    <h3>FeedBack Form</h3>
    <p>This is feedback form. Send us your feedback !</p>
    </div>
    <!-- Feedback Form -->
    <form action="#" id="form" method="post" name="form">
    <input name="vname" placeholder="Your Name" type="text" value="">
    <input name="vemail" placeholder="Your Email" type="text" value="">
    <input name="sub" placeholder="Subject" type="text" value="">
    <label>Your Suggestion/Feedback</label>
    <textarea name="msg" placeholder="Type your text here..."></textarea>
    <input id="send" name="submit" type="submit" value="Send Feedback">
    </form>
    <h3><?php include "secure_email_code.php"?></h3>
    </div>
    <!-- Feedback Form Ends Here -->
    </div>
    </body>
    <!-- Body Ends Here -->
    </html>
    

    PHP

    <?php
    if(isset($_POST["submit"])){
    // Checking For Blank Fields..
    if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["sub"]==""||$_POST["msg"]==""){
    echo "Fill All Fields..";
    }else{
    // Check if the "Sender's Email" input field is filled out
    $email=$_POST['vemail'];
    // Sanitize E-mail Address
    $email =filter_var($email, FILTER_SANITIZE_EMAIL);
    // Validate E-mail Address
    $email= filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!$email){
    echo "Invalid Sender's Email";
    }
    else{
    $subject = $_POST['sub'];
    $message = $_POST['msg'];
    $headers = 'From:'. $email2 . "rn"; // Sender's Email
    $headers .= 'Cc:'. $email2 . "rn"; // Carbon copy to Sender
    // Message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // Send Mail By PHP Mail Function
    mail("recievers_mail_id@xyz.com", $subject, $message, $headers);
    echo "Your mail has been sent successfuly ! Thank you for your feedback";
    }
    }
    }
    ?>
    

    CSS for the HTML Part

    @import "http://fonts.googleapis.com/css?family=Raleway";
    /*----------------------------------------------
    CSS Settings For HTML Div ExactCenter
    ------------------------------------------------*/
    h3,p,label {
    text-align:center;
    font-family:'Raleway',sans-serif;
    color:#006400
    }
    h2 {
    font-family:'Raleway',sans-serif
    }
    input {
    width:100%;
    margin-bottom:20px;
    padding:5px;
    height:30px;
    box-shadow:1px 1px 12px gray;
    border-radius:3px;
    border:none
    }
    textarea {
    width:100%;
    height:80px;
    margin-top:10px;
    padding:5px;
    box-shadow:1px 1px 12px gray;
    border-radius:3px
    }
    #send {
    width:103%;
    height:45px;
    margin-top:40px;
    border-radius:3px;
    background-color:#cd853f;
    border:1px solid #fff;
    color:#fff;
    font-family:'Raleway',sans-serif;
    font-size:18px
    }
    div#feedback {
    text-align:center;
    height:520px;
    width:330px;
    padding:20px 25px 20px 15px;
    background-color:#f3f3f3;
    border-radius:3px;
    border:1px solid #cd853f;
    font-family:'Raleway',sans-serif;
    float:left
    }
    .container {
    width:960px;
    margin:40px auto
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题