1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting email from MySQL db

Discussion in 'PHP' started by Wondwossen, May 23, 2018.

  1. #1
    I have a contact/inquiry form in my new website. I wanted this form to insert data into MySQL db, then to get every new form submission to my email, i.e. by pulling out from the db. The data insertion works well but I couldn't get the email from the database. I used separate php script files for both processes. Can anyone help me with this? Below here is the code.
    <?php
    $linkdb = mysqli_connect("localhost", "username", "password");
    if(!$linkdb){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }else{
        mysqli_select_db("my_database");
    }
    $query = "SELECT * FROM table_name";
    $result = mysqli_query($linkdb,$query);
    // Sending contact message from database via email
    if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_array($result)){
    // variables for sending emails
    $to = 'info@example.com, pr@example.com';
    $subject = 'Message from contact us form';
    $copyrecip = 'gm@example.com';
    // Body of the message
    $message = '<html><body>';
    $message .= '<h2><b>Inquiry Topic:</b>' . $row['inquiry_topic'] . '</h2>';
    $message .= '<p style="color:#031634;">Message:' . $row['message'] . '</p>';
    $message .= '<ul style="color:#01017e;">';
    $message .= '<b>Company name:</b>' . $row['company'] . '</li>';
    $message .= '<li><b>Region:</b>' . $row['region'] . '</li>';
    $message .= '<li><b>Location address:</b>' . $row['address'] . '</li>';
    $message .= '<li><b>Office phone:</b>' . $row['land_phone'] . '</li>';
    $message .= '<li><b>Mobile phone:</b>' . $row['mobile_phone'] . '</li>';
    $message .= '</ul></body></html>';
    $message .= stringwordwrap($message, 70, "\r\n");
    // Setting content-type header for an HTML email
    $headers = 'MIME-Version: 1.0' . "\r\n" .
    $headers = 'Content-type: text/html; charset=iso-8859-1';
    // Email headers
    $headers = 'From: '.$row['contact_name']."\r\n" .
                 'To: '.$to."\r\n".
                 'Cc: '.$copyrecip."\r\n".
                 'Reply-To: '.$row['contact_email']."\r\n" .
                 'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);
    }
    }
    //closing database connection
    mysqli_close($linkdb);
    ?>
    PHP:

    SEMrush
     
    Last edited by a moderator: Jun 5, 2018
    Wondwossen, May 23, 2018 IP
    SEMrush
  2. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #2
    You have the example of PHP code that gets the data from the database:
    So you could use it and get any data you need from the database. There should be no difficulties.
     
    Nei, Jun 4, 2018 IP
  3. Dreads

    Dreads Well-Known Member

    Messages:
    1,883
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    150
    #3
    Dreads, Jun 5, 2018 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,487
    Likes Received:
    422
    Best Answers:
    16
    Trophy Points:
    235
    #4
    Print (mysqli_num_rows to see how many rows have been selected...
     
    JEET, Jun 14, 2018 IP