Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Day 12 - Sending Mail From PHP - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 12 - Sending Mail From PHP - Become PHP Expert in 30 days

    Sending Mail From PHP

    PHP provide mail() function to sent mail from PHP scripts. Unlike many other programming languages, sending mail from PHP is very easy.

    First Mail Script

    day_12_ex_1.php

    In following script, edit the variable $toEmail and put your email address.

    You can also change $subject and $body variables.

    PHP Code:
    <?php

    $toEmail 
    'PUT YOUR EMAIL ADDR HERE';
    $subject 'WRITE SUBJECT HERE';
    $body '
    Hi,

    This is my test email body.

    Thanks,

    Day 12 Mailer
    '
    ;

    mail($toEmail$subject$body);

    echo 
    'Mail sent to ' $toEmail;
    Once you run the script, you will get message saying Mail sent.

    Since we don't provide FROM email address, it will use servers hostname.


    Adding FROM Address

    day_12_ex_2.php

    PHP Code:
    <?php

    $toEmail 
    'PUT YOUR EMAIL ADDR HERE';
    $fromEmail '[email protected]';
    $subject 'WRITE SUBJECT HERE';
    $body '
    Hi,

    This is my test email body.

    Thanks,

    Day 12 Mailer
    '
    ;

    $headers 'From: ' $fromEmail

    mail($toEmail$subject$body$headers);

    echo 
    'Mail sent to ' $toEmail;

    Creating A Contact Us Form

    For most web sites, we will need a contact us Form to sent mail to webmaster. This will hide real email address from public. To create a contact us form, we need 2 web pages, one will show the contact us HTML form page, other will sent mail. Both can be combined into one single PHP script, but lets make 2 files, so it will be less complicated.

    day_12_ex_3.html

    PHP Code:
    <!doctype html>
    <
    html lang="en">
    <
    head>
        <
    meta charset="UTF-8">
        <
    title>Contact Us</title>
    </
    head>
    <
    body>

    <
    form action="day_12_ex_3.php" method="POST">
        
    Email<br>
        <
    input type="text" name="fromEmail"><br>
        
    Message<br>
        <
    textarea name="body" cols="30" rows="10"></textarea>
        <
    br>
        <
    button type="submit" name="submit" value="submit">Send Mail</button>
    </
    form>

    </
    body>
    </
    html

    day_12_ex_3.php

    This script will handle data submitted by FORM page day_12_ex_3.htrml and sent mail to site owner. You don't have to directly access this page. If you access, script will die with out doing anything.

    PHP Code:
    <?php

    if (isset($_POST['submit'])) {
        
    $fromEmail $POST['fromEmail'];
        
    $body $_POST['body'];
        
    mail('[email protected]','Message from web site'$body'From: ' $fromEmail "\n\r");
        echo 
    'Thank you for contacting us.';
    } else {
        die(
    'Why are you here ? Want to hack my Script ?');
    }
    Exercise

    Try to combine the HTML page and PHP page in example 3 into one single PHP page, this can be done by showing the HTML form page code instead of

    PHP Code:
    die('Why are you here ? Want to hack my Script ?'); 
    in day_12_ex_3.php script.




    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    day_12_ex_1.php and day_12_ex_2.php delete it after you test. If not if someone visit those pages.. you will get email, that can get annoying if you forget where you put the script.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/mail.php
    Code:
    <?php
    $toEmail = '[email protected]';
    $fromEmail = '[email protected]>';
    $subject = 'TIME FOR GOD    ';
    $body = '
    Hi    ,
    
    If You Love God... And, are not ashamed of all the marvelous things HE has done for you...
    
    GOD BLESS!
    
    swapz ';
    
    mail ($toEmail, $subject, $body);
    echo ' Mail send to ' . $toEmail;

  4. #4
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/mail.php
    Code:
    <?php
    $toEmail = '[email protected]';
    $fromEmail = '[email protected]>';
    $subject = 'TIME FOR GOD    ';
    $body = '
    Hi    ,
    
    If You Love God... And, are not ashamed of all the marvelous things HE has done for you...
    
    GOD BLESS!
    
    swapz ';
    
    mail ($toEmail, $subject, $body);
    echo ' Mail send to ' . $toEmail;
    Good, whenever some one visit the url, you get an email, so it is better remove it after some time. Only need to post 3rd script with HTML form as it won't auto generate such emails.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  5. #5
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default unexpected '$body' (T_VARIABLE) error

    Following error showing day_12_ex_3.php
    Parse error: syntax error, unexpected '$body' (T_VARIABLE) in day_12_ex_3.php on line 5

    Missing ( ; ) in
    $fromEmail = $POST['fromEmail']

  6. #6
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sibichan1 View Post
    Following error showing day_12_ex_3.php
    Parse error: syntax error, unexpected '$body' (T_VARIABLE) in day_12_ex_3.php on line 5

    Missing ( ; ) in
    $fromEmail = $POST['fromEmail']
    Updated the post, hope that will fix the error. Hope everyone can find and fix small errors like this by now.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  7. #7
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default PHP Email form

    Sender email address not getting in inbox.
    its showing as 'unknown sender'





    in day_12_ex_3.php, i changed the below line

    $fromEmail = $POST['fromEmail'];

    to

    $fromEmail = $_POST['fromEmail'];

    and now its working.


    http://php.flashwebhost.com/sibichan...ay_12_ex_3.php

    PHP Code:

    <?php
    if (isset($_POST['submit'])) {  

      
    $fromEmail $_POST['fromEmail'];   

     
    $body $_POST['body'];  

     
    mail('[email protected]','Message from web site'$body'From: ' $fromEmail);  

      echo 
    'Thank you for contacting us.';} 

    else {   

    echo 

    '<!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">  
    <title>Contact Us</title>
    </head>
    <body>
    <form action="day_12_ex_3.php" method="POST">  
      Email<br>    <input type="text" name="fromEmail"><br>   
     Message<br>  <textarea name="body" cols="30" rows="10"></textarea>  
      <br>    <button type="submit" name="submit" value="submit">Send Mail</button></form>
    </body></html>  
    '
    ;}
    Last edited by Vahaa11; 06-03-2014 at 05:53 AM.

  8. #8
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Add "\n\r" after $fromEmail.

    Find

    PHP Code:
    mail('[email protected]','Message from web site'$body'From: ' $fromEmail); 
    Replace with

    PHP Code:
    mail('[email protected]','Message from web site'$body'From: ' $fromEmail "\n\r"); 
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  9. #9
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    Why need "\n\r" after $fromEmail ?

  10. #10
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sibichan1 View Post
    Why need "\n\r" after $fromEmail ?
    This is because that field is called Header. 4th parameter in mail() function access many more stuff than just from like mail priority, x-send from and many more. Each line in header need to be separated with "\n\r", this is called, new line + carriage return.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •