How to configure PHP to send e-mail?

后端 未结 8 1578
星月不相逢
星月不相逢 2020-12-10 07:12

I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:

  $to = \"myweb@gmail.co         


        
相关标签:
8条回答
  • 2020-12-10 07:23

    Here's the link that gives me the answer and we use gmail:

    Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

    Modify the php.ini file to use it (commented out the other lines):

    mail function

    For Win32 only.

    SMTP = smtp.gmail.com
    smtp_port = 25
    For Win32 only.
    sendmail_from = <e-mail username>@gmail.com
    

    For Unix only.

    You may supply arguments as well (default: sendmail -t -i).

    sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
    

    (ignore the "Unix only" bit, since we actually are using sendmail)

    You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

    sendmail

    smtp_server=smtp.gmail.com
    smtp_port=25
    error_logfile=error.log
    debug_logfile=debug.log
    auth_username=<username>
    auth_password=<password>
    force_sender=<e-mail username>@gmail.com
    
    0 讨论(0)
  • 2020-12-10 07:24

    Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer

    How to use it:

    require('./PHPMailer/class.phpmailer.php');
    $mail=new PHPMailer();
    $mail->CharSet = 'UTF-8';
    
    $body = 'This is the message';
    
    $mail->IsSMTP();
    $mail->Host       = 'smtp.gmail.com';
    
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;
    $mail->SMTPDebug  = 1;
    $mail->SMTPAuth   = true;
    
    $mail->Username   = 'me.sender@gmail.com';
    $mail->Password   = '123!@#';
    
    $mail->SetFrom('me.sender@gmail.com', $name);
    $mail->AddReplyTo('no-reply@mycomp.com','no-reply');
    $mail->Subject    = 'subject';
    $mail->MsgHTML($body);
    
    $mail->AddAddress('abc1@gmail.com', 'title1');
    $mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */
    
    $mail->AddAttachment($fileName);
    $mail->send();
    
    0 讨论(0)
  • 2020-12-10 07:24

    configure your php.ini like this

    SMTP = smtp.gmail.com
    
    [mail function]
    ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
    
    ; SMTP = smtp.gmail.com
    
    ; smtp_port = 465
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = postmaster@localhost
    
    0 讨论(0)
  • 2020-12-10 07:26

    You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.

    0 讨论(0)
  • 2020-12-10 07:34

    You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.

    If you own a server or VPS upload the script and it will work fine.

    0 讨论(0)
  • 2020-12-10 07:36

    This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.

    <?php
    if (isset($_POST['name']) && isset($_POST['email'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $to = 'your.email@address.com';
        $subject = "New Message on YourWebsite.com";
        $body = '<html>
                    <body>
                        <h2>Title</h2>
                        <br>
                        <p>Name:<br>'.$name.'</p>
                        <p>Email:<br>'.$email.'</p>
    
                    </body>
                </html>';
    
    //headers
    $headers = "From: ".$name." <".$email.">\r\n";
    $headers = "Reply-To: ".$email."\r\n";
    $headers = "MIME-Version: 1.0\r\n";
    $headers = "Content-type: text/html; charset=utf-8";
    
    //send
    $send = mail($to, $subject, $body, $headers);
    if ($send) {
        echo '<br>';
        echo "Success. Thanks for Your Message.";
    } else {
        echo 'Error.';
    }
    }
    ?>
    
    <html>
        <head>
            <meta charset="utf-8">
        </head>
        <body>
            <form action="" method="post">
                <input type="text" name="name" placeholder="Your Name"><br>
                <input type="text" name="email" placeholder="Your Email"><br>
                <button type="submit">Subscribe</button>
            </form>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题