user1083148
user1083148

Reputation: 109

HTML form with Send button to "Email" field's address and not to site admin

With a contact form, a customer usually fills in their information and email etc., and when they click Send, an email is sent to you.

However, I need a form where it sends the email to the email that is populated in the "Email" field.

I have to send tracking information daily to customers and would like to do it from this form instead of an email client.

 <div id="form">
 <div id="name">
 <p id="username"> Name:&nbsp; </p>
 <input type="text" name="name" class="textfield">
 </div>
 <div id="name">
 <p id="username"> Email: &nbsp; </p>
 <input type="text" name="name" class="textfield">
 </div>
 <div id="name">
 <p id="username"> Message: </p>
 <input type="text" name="message" class="textarea">
 </div>
 <input type="button" value="SEND" id="btn"> 
 </div>

I do not know how to do the final part where the SEND function fires the correct way. If anyone can help with that please.

Upvotes: 1

Views: 2232

Answers (1)

Jesperhag
Jesperhag

Reputation: 313

I would solve this with the use of Swiftmailer. The form would look like the followning:

<form method="POST" action="send.php">
<div id="form">
<div id="name">
<p id="username"> Name:&nbsp; </p>
<input type="text" name="name" class="textfield">
</div>
<div id="name">
<p id="username"> Email: &nbsp; </p>
<input type="text" name="email" class="textfield">
</div>
<div id="name">
<p id="username"> Message: </p>
<input type="text" name="message" class="textarea">
</div>
<input type="button" value="SEND" id="btn"> 
</div>
</form>

Then the 'send.php' would look like:

<?php
require_once 'lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.google.com', 465, 'ssl')
  ->setUsername('[email protected]')
  ->setPassword('your password');

$mailer = Swift_Mailer::newInstance($transport);

$name = $_POST['name'];
$emailTo = $_POST['email'];
$message = $_POST['message'];


$message = Swift_Message::newInstance('Your subject goes here')
  ->setFrom(array('[email protected]' => 'Your name here'))
  ->setTo(array($emailTo => $name))
  ->setBody($message);


$result = $mailer->send($message);

if ( $result > 0 )
{
    //Email was sent
}
else
{
    // Email was not sent
}

?>

Swiftmailer you can download from here: http://swiftmailer.org/download (just unzip to the same path as send.php) Documentation of Swiftmailer is here: http://swiftmailer.org/docs/introduction.html

Hope it helps!

Upvotes: 3

Related Questions