Hi,
I'm sure this is very simple but am unable to find out how.
How do I display an email address as a mailto link like in HTML? I have records displayed from a mysql db but would like to display a member email address that acts as a mailto link. Below displays the code that echos the value but of course I would like the value (email address) to function as a mailto link in HTML

<?php echo $row_master_view['email']; ?>

Thanks in advance.

I assume you want to mail on click of this link, if so, execute the php script to send a mail using ajax on click of this link.

I assume you want to mail on click of this link, if so, execute the php script to send a mail using ajax on click of this link.

That's right. I want to open the users email client to send mail, as the mailto function does in HTML. Could you modify the code or provide an example of how this is done? Thanks for your quick response.

var req = createXMLHttpRequest();

function createXMLHttpRequest() {
 var ua;
 if(window.XMLHttpRequest) {
 try {
  ua = new XMLHttpRequest();
 } catch(e) {
  ua = false;
 }
 } else if(window.ActiveXObject) {
  try {
	ua = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e) {
	ua = false;
  }
 }
return ua;
}

function sendRequest()
{
	//var rnd982g = Math.random();
//	var str = "";
	var url= 'send_mail.php';
	if(document.getElementById('txtName') !='' && document.getElementById('txtName') != null)
	{
		url +="?name="+document.getElementById('txtName').value;
	}
	if(document.getElementById('txtMail') !='' && document.getElementById('txtMail') != null)
	{
		url +="&id="+document.getElementById('txtName').value;
	}else
	{
		alert("enter email address");
	return false;
	}
	if(document.getElementById('txtBody')!='' && document.getElementById('txtBody')!=null)
	{
		url+="&body="+urldocument.getElementById('txtBody').value;
	}else
	{
		alert("email body is empty");
		return false;
	}
	
	req.open('GET', url);
	req.onreadystatechange = handleResponse;
	req.send(null);
	return false;
}

function handleResponse() {
 if(req.readyState == 4){
  var response = req.responseText;
  document.getElementById("results").innerHTML = response;
 }
}
</script>
<? ?>
<html>
<head></head>
<body>
<form name="frm" id="frm" method="post" target="">
<table>
<tr><td>Name</td><td><input type="text" name="txtName" id="txtName" /></td></tr>
<tr><td>Email Address</td><td><input type="text" name="txtMail" id="txtMail" /></td></tr>
<tr><td>Body</td><td><textarea name="txtBody" id="txtBody" ></textarea></td></tr>
<tr><td><input type="submit" value="Send Mail" onclick="sendRequest();" /></td></tr>
</form>
</body>
</html>

Now you need the send_mail.php to send the actual mail

Now save this as send_mail.php-

<?
$name = $_GET['name'];
$email_id = $_GET['id'];
$body = $_GET['body'];
$subject = "This is the subject";

if(mail($email_id,$subject,$body))
{
	echo "Done";
}
else{ echo "Failed";}

?>

Why not let the browser handle it:

<?php 
  echo '<a href="mailto:' . $row_master_view['email'] . '">' . $row_master_view['email'] . '</a>';
?>

yes, its cool too, but the process by ajax will be done in the backend.
page won't refresh while sending the mail.

yes, its cool too, but the process by ajax will be done in the backend.
page won't refresh while sending the mail.

Reread the first post and the title.

The OP wants a mailto link, not a PHP mail() function.

Why not let the browser handle it:

<?php 
  echo '<a href="mailto:' . $row_master_view['email'] . '">' . $row_master_view['email'] . '</a>';
?>

Exactly what I was after. Thankyou very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.