简体   繁体   中英

build html link with variable inside php mail

Using the PHP mail function I need to send out an email that includes a link back to a record that has been modified, the link will need to contain the $id of the record that was modified on the database.

So far I have the email built, however am not sure on how I can include the link with variable.

mail('test@domain.com', 'Visitor Record Updated', "Hello,\r\n\r\nThe visitor record for " . $firstname . " " . $lastname . " Has been updated.\r\n\r\nYou can view the changes" . $url ."\r\n\r\nThank You");

That is how I plan to have the email sent, however how can i build the $url variable so that it sends the link as

http://app.site.com/visitor-view.php?id=$id
mail('test@domain.com', 'Visitor Record Ppdated', "Hello,\r\n\r\nThe visitor record for " . $firstname . " " . $lastname . " Has been updated.\r\n\r\nYou can view the changes: http://app.site.com/visitor-view.php?id=" . $id ."\r\n\r\nThank You");

You can send simple link. But it may not clickable in some email provides. So You have to send as html email:

Refer below link for more details

http://css-tricks.com/sending-nice-html-email-with-php/

If you want to add CSS, then you can add as inline style.

From above link i prepared for your question:

** prepare content of email**

$content ="<html><head><title>Welcome to Mysite</title></head>
<body>
<p>Hello,<br/><br/>
  The visitor record for " . $firstname . " " . $lastname . " Has been updated.<br/><br/>
  You can view the changes <a href='".$url."'>Here</a></p>

<p>Thank You</p>
</body></html>"; 


$to = 'test@domain.com';

$subject = 'Visitor Record Updated';

$headers = "From: noreply@domai.com\r\n";
$headers .= "Reply-To: info@yourdomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $subject, $content, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM