Solved

Sending Email From Webpage

Posted on 2013-06-24
6
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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.

752 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