Solved

Sending Email From Webpage

Posted on 2013-06-24
6
232 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 109

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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