Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
PHP file() and email?
I want to send an HTML email with PHP and basically I want to just send an html file I have on the website. However, the file I want to send has PHP in it to personalize the email with the name and stuff: Hi <? echo $name;?>, like that. So can I read it into an array like: $file = file('email.html'), and will the php part still work when I send it, meaining will it look like: Hi John?
Also, is there any good site to test PHP where I can just type in my code and run it, kind of like the Try It Editor for HTML.
Any help is appreciated and 10 points goes to somebody who can competently answer both questions (or if no one does the one who has...the best answer!). Thanks so much!
2 Answers
- 1 decade agoFavorite Answer
Part 1a.
It's not too hard to send an email using php, even one that contains html.
Here is the code you would need
$to = "blah@yahoo.co.uk";
$subject = "Email Subject";
$message = "Your HTML CODE HERE";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: FINDEQUINE.CO.UK";
mail($to,$subject,$message,$headers);
Part 1b...
With regards to the php content, I would say it could possibly be done (i'm not sure how though as the code would not be on a server) the way you describe but I could forsee security issues.. There is a much better, easier and safer way.
You can store the info into a large string as you suggest but build it out of a series of strings which are generated there and then in the script you are using to send the email so that this script adds the personal touches like the name and then sends the lot as a regular html email.. That might not make much sense I wonder if I can try to give you an example. Say you have your name stored in $name as your example shows. To send an email with the name aready set you would set the $message variable like this to give you a simple html page saying hello john
$message='<html><head></head>
<body>
<p>Hello '.$name.' and welcome etc</p></body></html>';
Note the above is one line, it just wouldn't show it all here unless i split it onto 3 lines...
and the actual code the email would contain is just html but personalised as you would want. Keeps all the php code on the server, this is the normal way to do things.
Part 2...
The easiest way to have a way to test your code is to install a WAMP or LAMP (Windows, apache, mysql & php or Linux, Apache etc)on your computer. For a point and click installation with small unobtrusive download, I recommend XAMPP which is aimed at 'off line' prototyping. Just download, click to install and it will do the rest. You will have a subdirectory called www or htdocs or similar on your harddrive within the directory the package is installed. You copy your file into this folder, eg mytest.php and then go into your browser and go to the address http://localhost/mytest.php and it will run the script. This is the best way to prototype code and also means if you make a mistake and lock up the server, because you have run it on your own pc first, you won't be penalised or fined by your webhost cause you won't have locked up theirs....
Hope that helps. I provide a link to xampp below and w3schools which is a great free learning resource for html, php, javascript and much more. Have fun :-)
- 5 years ago
With a "mailto" command? Are you ***? You have php: use "mail()". Much better. More professional. Leave "maitlo" to those who can't code.