Send email with PHP from html form on submit with the same script

前端 未结 8 1869
情深已故
情深已故 2020-11-22 01:22

I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that disp

相关标签:
8条回答
  • 2020-11-22 01:48

    If you haven't already, look at your php.ini and make sure the parameters under the [mail function] setting are set correctly to activate the email service. After you can use PHPMailer library and follow the instructions.

    0 讨论(0)
  • 2020-11-22 01:50

    PHP script to connect to a SMTP server and send email on Windows 7

    Sending an email from PHP in Windows is a bit of a minefield with gotchas and head scratching. I'll try to walk you through one instance where I got it to work on Windows 7 and PHP 5.2.3 under (IIS) Internet Information Services webserver.

    I'm assuming you don't want to use any pre-built framework like CodeIgniter or Symfony which contains email sending capability. We'll be sending an email from a standalone PHP file. I acquired this code from under the codeigniter hood (under system/libraries) and modified it so you can just drop in this Email.php file and it should just work.

    This should work with newer versions of PHP. But you never know.

    Step 1, You need a username/password with an SMTP server:

    I'm using the smtp server from smtp.ihostexchange.net which is already created and setup for me. If you don't have this you can't proceed. You should be able to use an email client like thunderbird, evolution, Microsoft Outlook, to specify your smtp server and then be able to send emails through there.

    Step 2, Create your Hello World Email file:

    I'm assuming you are using IIS. So create a file called index.php under C:\inetpub\wwwroot and put this code in there:

    <?php
    
      include("Email.php");
    
      $c = new CI_Email();
    
      $c->from("FromUserName@foobar.com");
      $c->to("user_to_receive_email@gmail.com");
      $c->subject("Celestial Temple");
      $c->message("Dominion reinforcements on the way.");
      $c->send();
      echo "done";
    ?>
    

    You should be able to visit this index.php by navigating to localhost/index.php in a browser, it will spew errors because Email.php is missing. But make sure you can at least run it from the browser.

    Step 3, Create a file called Email.php:

    Create a new file called Email.php under C:\inetpub\wwwroot.

    Copy/paste this PHP code into Email.php:

    https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php

    Since there are many kinds of smtp servers, you will have to manually fiddle with the settings at the top of Email.php. I've set it up so it automatically works with smtp.ihostexchange.net, but your smtp server might be different.

    For example:

    1. Set the smtp_port setting to the port of your smtp server.
    2. Set the smtp_crypto setting to what your smtp server needs.
    3. Set the $newline and $crlf so it's compatible with what your smtp server uses. If you pick wrong, the smtp server may ignore your request without error. I use \r\n, for you maybe \n is required.

    The linked code is too long to paste as a stackoverflow answer, If you want to edit it, leave a comment in here or through github and I'll change it.

    Step 4, make sure your php.ini has ssl extension enabled:

    Find your PHP.ini file and uncomment the

    ;extension=php_openssl.dll
    

    So it looks like:

    extension=php_openssl.dll
    

    Step 5, Run the index.php file you just made in a browser:

    You should get the following output:

    220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at 
    Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0 
    <534edd7c92761@summitbroadband.com> Queued mail for delivery 
    lang:email_sent
    
    done
    

    Step 6, check your email, and spam folder:

    Visit the email account for user_to_receive_email@gmail.com and you should have received an email. It should arrive within 5 or 10 seconds. If you does not, inspect the errors returned on the page. If that doesn't work, try mashing your face on the keyboard on google while chanting: "working at the grocery store isn't so bad."

    0 讨论(0)
提交回复
热议问题