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
2 Answers 2
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:
$content =" Hello,
The visitor record for " . $firstname . " " . $lastname . " Has been updated.
You can view the changes Here
Thank You
"; $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);
Send HTML links in php mail message
I am trying to send a html link in mail using php. But I am getting the full text of html in the mail. Below is the code what I have tried . But it is not working. Any help is appreciated.
$emailid = $_POST['email']; $phoneno = $_POST['phoneno']; $to = "demoemail@gmail.com"; // this is your Email address $from = $emailid; // this is the sender's Email address $subject = "Intrested in scheduling appointment."; $message = "Click Here"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers = "From:" . $from; $retval = mail($to,$subject,$message,$headers);
2 Answers 2
I’ve checked with your code..
Seems like you are making a simple mistake.. You are not concatenating your code.. you are re assigning your header at line 3
$emailid = $_POST['email']; $phoneno = $_POST['phoneno']; $to = "demoemail@gmail.com"; // this is your Email address $from = $emailid; // this is the sender's Email address $subject = "Intrested in scheduling appointment."; $message = "Click Here"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers = "From:" . $from; $retval = mail($to,$subject,$message,$headers);
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From:" . $from;
Update your header and try again!
Working code with template paste and run
Hi google
Just one more click
Activate Account
Kind Regards,
The google Team
google - The leading marketplace selling google.
This account notification was sent to you because you are a google user. Should you have any questions, you are welcome to email us.
Copyright © 2017 | google Pty Ltd. All rights reserved.
'; $header = "From:abc@gmail.com \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) < echo "Message sent successfully. "; >else < echo "Message could not be sent. "; >?>
This question is in a collective: a subcommunity defined by tags with relevant content and experts.
send link using php mail() function
User should get a email in the format:—— Copy the token: xxxxx And paste it in this Link —— My codes:
4 Answers 4
seems like your email function doesn’t have good react with html tags, you probably missing this in you’r $header object.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
you want me to copy paste that context from this site ? the main problem is that people doesn’t google before they open new question.
No, you should add some code from that site and explain it how, why, when it works best of OP based on the link you are posting.
i googled a good many times. i tried as they said. even answers from stack overflow doesn’t work for me. but those answers worked for the askers.
There is many points you should think about them .. you can notice them in the following example code:
Link"; $random_hash = md5(uniqid(time())); $header = "From: $fromTitle \r\n"; $header .= "Reply-To: ".$emailFrom."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$random_hash."\"\r\n\r\n"; $header .= "This is token email.\r\n"; $header .= "--".$random_hash."\r\n"; $header .= "Content-type:text/html; charset=UTF-8\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; @mail($emailTo, $subject, $message, $header); ?>
I don’t know why someone make vote down .. if there is some thing wrong just let us know .. my answer is tested and works correctly
yes i agree we must use variable for this .. i will change my answer .. and your function token_generator as i think should return the token and not echo the token
Upvoted. This is a great and complete answer. The only things I want to mention are: I think you are misusing boundary (see this article by Kevin McMahon: goo.gl/Li1jJ); and $from should be changed to $fromTitle in $header = «From: «.$from // . (I’ve submitted an edit to your answer for this). Aside from that, great answer. Prior to reading this, I didn’t even know about the multipart/mixed MIME type.
@SpencerDoak okay let me clear with you ,, i faced many problems before with send email ,, and after searching i found this as the best solution for me but i do not know what is the meaning of each stuff ,, you know ,, sometimes you do not have time ,, you just need to solve the issue and save your day .. i must read more about it 😉
There are some problems in your strings, however, here are a few things to consider here:
First all you might want to (just as a matter of practice) assign token_generator(10) to a variable on your script.
and pass $token to the script
Secondly you are having some trouble with the strings in your email. You don’t need to wrap your link in a href tags (in fact it will be a problem if the user does not have HTML email turned on (as many do not). Just post the link itself.
Thirdly, why not just attach the token to the URL? It’s an easier step for the user. Here’s an example:
$message="Follow this link: http://(url)recover_pass_get_mail.php?token=$token";
and then you can just get the token from the URL using
Include html in email
I suggest using PHPMailer, easy to use, takes care of all nesseccery headers, easy attachment sending, multiple recipients etc.
This supports multipart emails. +1 for being the only person to point to a solution that does that so far.
Set the correct headers (from php.net)
// To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Mail it mail($to, $subject, $message, $headers);
your $message may now contain HTML. For complex html/emails, it’s advisable to look at some packages such as the PEAR Mailer class for instance.
@Visa my code is taken straight from the PHP.net’s page on mail — have a good read of that and the comments.
I didn’t understand. You need to echo to the html like this ?
Or do you need to do this :
What’s exactly that you are trying to do ?
If you are trying to send HTML data via mail(), you need to set few headers
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf8' . "\r\n"; mail($to, $subject, $body, $headers);
For more information — check http://php.net/manual/en/function.mail.php example 4
Here are the birthdays upcoming in August!
Person Day Month Year Joe 3rd August 1970 Sally 17th August 1973