Solved

Sending Email From Webpage

Posted on 2013-06-24
6
229 Views
Last Modified: 2013-06-25
I want to give a user the ability to send themselves an E-Mail from a webpage.  I also want the Name. Address, Phone of the restaurant to be in the content part of the E-Mail.  This info comes from an online MySQL database.  How would I do this?
0
Comment
Question by:DS928
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39272099
Fetch the required fields from database as you write you want name, address etc. Keep them in a variable and you can use simple php mail function like:

<?php
$to      = 'to@example.com';
$subject = 'the subject';
$message = 'Hello';
$message.= "Name". $name."\r\n";
$message.= "Address".$address."\r\n";//Like this line you can add name address value in message.


$headers = 'From: to@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Open in new window

0
 

Author Comment

by:DS928
ID: 39272217
Thank you.  I am still having difficulty.  Go to this page..
http://www.menuhead.com/bigsearch.php

Selct Baltimore / American / Click on Arrow.......
Select 13.5% Winebar

Towards the bottom right I have an Email Button.  I want the ability for the user to Email the Name and Address of the restaurant to themseleves.  Here is the code behind the button.... $Lid is the variable that contains the ID of the restaurant....

<td width="300"><div align="center">
	<a href="mailto:' . $Email . '">' . $Email . '</a>
	<?php
		$to      = 'to@example.com';
		$subject = 'the subject';
		$message = 'Hello';
		$message.= "Name". $name."\r\n";
		$message.= "Address".$address."\r\n";//Like this line you can add name address value in message.


		$headers = 'From: to@example.com' . "\r\n" .
    	'Reply-To: webmaster@example.com' . "\r\n" .
    	'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	?>;
    <img src="Images/Buttons/BigRestMail.png" alt="" width="220" height="70" border="0"/></a></div></td>

Open in new window

0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39272263
You did not read my post carefully, You are using variable in single quote that is problem do not use $email in '$email' it will be as a string just use $email only.

$message.= "Name". $name."\r\n"; 

Open in new window

You need to add variable which contain name here in $name, like rest of the works address and others.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:DS928
ID: 39272324
Like this?

<a href="mailto: . $Email . "> . $Email . </a>

But still not opening.
0
 

Author Comment

by:DS928
ID: 39272632
Ok, The outlook link is working.  But the Subject , Message and Header is not populating.

<td width="300"><div align="center">
	<a href="mailto:">
    <?php
		$Lid = 'Lid';
		$to      = 'to@example.com';
		$subject = 'MENUHEAD.COM';
		$message = 'Hello';
		$message.= "Name". $name."\r\n";
		$message.= "Address".$address."\r\n";//Like this line you can add name address value in message.


		$headers = 'From: to@example.com' . "\r\n" .
    	'Reply-To: webmaster@example.com' . "\r\n" .
    	'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	?>
    <img src="Images/Buttons/BigRestMail.png" alt="" width="220" height="70" border="0"/></a></div>
    </td>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39273069
Here is the essential example of the form-to-email script.  Note that the first thing you will find with any such script that allows a client to enter the email will be that the script will immediately come under attack from spammers who are looking for an open relay.  Be absolutely certain that you lock the script down with every security measure you can use!.

To get the "To" address into the mail command, you would just add it to the HTML form and pick the data out of the POST request array.

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // DISABLED ON THE SERVER SIDE
    var_dump($_POST);
    die(' DISABLED');

    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now