eric February 2016

How to send an email to the email-address saved from the database?

I am trying to make an email form in my website wherein the members of that website will be able to contact us through email, but I do not know how to get the email address I have from my database where they will send the mail.

I hope someone could help me. So far I have this.

contact.php

<form method="post" action="send_email.php">

    <label>First Name* &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</label><input type="text" placeholder="fn" name="fn" id="fn" maxlength="80" />
    <br>
    <label>Last Name* &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</label><input type="text" placeholder="ln" name="ln" id="ln" maxlength="80" />
    <br>
    <label>Email-address* &nbsp;&nbsp;</label><input type="email" placeholder="Email" name="email" id="email" maxlength="200"/>
    <br />
    <label>Message* </label><br/> <br/><textarea name="message" id="message" rows="4" cols="50" maxlength="100"></textarea>
    <input type="hidden" name="submit" id ="submit" >
    <right><input type="submit" name="submit" value="Send Email" class="btn-style" /></right><br>
</form>

And this is my send_email.php

<?php
require_once('db.php');

$id= $_GET["id"];
$result = mysqli_query($conx, "SELECT email from contact where id='$id'");
while($row = mysqli_fetch_array($result)) {
    $to = $row["email"];
    }

if ($_GET["submit"]) {  
    $fn = $_POST['fn'];
    $ln = $_POST['ln'];
    $mailfrom = $_POST['email'];
    $message = $_POST['message'];

    $headers = "From:" . $fn . " " . $ln . "\r\n" . $mailfrom;
    if (mail($to, $message, $headers)) {

     echo "<script type='text/javascript'>alert('Your email has been sent! Thank you for contacting us!');window.location.href='co        

Answers


Corovino February 2016

You can do a test directly to a BD using query id and if you bring the mail, you can now move on to php SQL: select email from MyTbl where id = 34.

Also I think you're missing the mail($ to, $ message, $ headers, $ MailFrom), add mail to the person who wants to come email

Post Status

Asked in February 2016
Viewed 2,596 times
Voted 11
Answered 1 times

Search




Leave an answer