What is the best way for creating default emails? - php

I'm planing on using PHPMailer which comes along with Wordpress.
I will be submitting HTML e-mail including alternative txt mail using SMTP.
I will have different e-mails depending on user action (registration, request etc).
I'm not sure if this is the best way, but I'm thinking of creating a folder containing HTML files, e.g. /emails/confirm_registration.html.
Then I will fetch the content and put it in the body of the mail $mail->Body=$message;
I also want to include variables such as the users name in the body text.
So this is my question:
Should I put the body text inside a function, call it with parameters and then return content?
Or should I add placeholders like {first-name} in the HTML and use str_replace('{first-name}','Some name') once I've returned the html content?
If you have better suggestions, I would greatly appreciate that.

May I suggest you use SwiftMailer as your email library
http://swiftmailer.org/
This library is really stable, elegant and complete. Your method of replacing placeholders with text is standard and works, if I recall many common templating libraries like smarty for example use a very similar methodology.

I always use this method in my custom programming, it seems perfect to me.

Related

Dangers of using strip_tags in php forms?

I created a php form that uses strip_tags to send mail to the client. The form is not connected to any database.
What are the potential risks of malicious use?
The function strip_tags is used to filter out tags that might otherwise render something on the screen.
So it prevents the use of images, iframes, tables, layout.
This is probably not what you want.
You can however add a second parameter to the function with a list of tags that are allowed so you can still keep the tags you need.
I don't think this is really handy if only you send the emails. If people using your website have the ability to send emails then it might be of help

PHPMailer HTML with a variable using BCC

I am using PHPMailer, I have set it all up and working everything is fine, however I have run into a problem.
I need for each recipient to receive the HTML slightly different which is a link within the HTML email. So the link would have to change for each recipient.
I could simply use a php loop that sends it one by one to each recipient, however this will take a lot of processing and could time out the request I do not want this to happen.
Is there away I can use shortcodes using curly brackets as you would on most wysiwyg editors {email} so then I do $mail->send() once as oppose to loop through all of the recipients and do $mail->send() for each one, which I am trying to avoid.
If you need any more information I am happy to edit this question.
https://code.google.com/a/apache-extras.org/p/phpmailer/
I don't think you can do that using only PHPMailer. I had a look at the class methods, nothing linked to such a thing.
Worx International Inc. has another solution called PHPMailer-ML that could provide a good solution to your needs : http://phpmailer.worxware.com/index.php?pg=phpmailerml

php email edit interface and save method

I've created a simple information system which, among other thins, sends automatic html emails.
The body of this emails is defined by me within the php code, where it is saved, and the automatic routine is done by a cron job.
The problem is that, every time I want to change anything to these standard emails, I have to edit a .php file, since I am the developer and the only one capable of doing it. So, what I want now is to create a simple interface on the front-office of the IS so that the administrators can view and edit themselves these emails.
How can I do this? What is the best save method for the body of this emails (mysql, html file, ...) ? Is there any html editor that I could use in my website?
Thanks in advance for your help.
Regards,
MH
You'd probably be best off saving the data in a database (possibly MySQL), and if you can, build something to edit the content, or use something such as CKEditor or TinyMCE to modify the content.
If you already have a database set up, that would be a good way to do it. For an editor, I have used TinyMCE in the past and it has worked out well.
http://www.tinymce.com/
I would recommend you to store the body on the Database using for example MySQL and then updating it with a form + php.
You can use as Shamil say, some rich text editors such as TinyMCE which will deal with the HTML code to generate the rich text body.

Include whole page in php mail

Is it possible to include a separate html page in a php email?
Example, create a separate page that has all the content on it I want to email it and then include it in a php mail() on another page as the message?
Thanks
If you meant sending HTML email, yes. You can do that. You just need to add additional headers in the fourth parameter of mail() and pull the information from the HTML file you have using file_get_contents(). Please check this:
http://css-tricks.com/sending-nice-html-email-with-php/
Yes, it's possible exactly how rationalboss explained. Personally I use PEAR for html emails. It's a pain to get started with if you're not familiar to PEAR, but life has improved once I got it going.
Careful though, html/css does not have the same support in email clients that it does in browsers. You can create a beautiful page and find out that most people receive it with huge glitches due to certain CSS rules being ignored.
Best practice last I heard is to keep it simple and use inline styles for everything. Avoid floats and positioning. In fact, I believe it is actually still safer to use tables when dealing with email layouts if you need things to sit next to each other.
And then test in as many email clients as you can. Then cry and try to fix things.
I hear good things about Email on Acid for testing. It's a pay service but they offer a limited test for 3 clients as well.
If you're just doing a relatively simple email then it shouldn't be too bad. But if you're trying to make something that really looks great I recommend doing some googling on styling html specifically for emails.

PHP displaying html email in a html page

I'm building an PHP email mailbox script.
How would I make html emails display cleanly as they do in gmail/hotmail.
If I just echo it out it affects the whole page layout.
I could use iframes but surely that isn't the best solution.
If you are looking for the 'best solution' get on board with another open source email library that is doing the same thing you are. Maintaining an email renderer on your own that is safe against script injection and other hacks will simply be too much work for one person.
One example: https://github.com/afterlogic/webmail-lite
Another: http://trac.roundcube.net/
You get the benefit of other developers who use the library maintaining the code base, so if something is broken, all you have to do is pull the latest update (hopefully) and you get the fix. If you find something that needs improving, you can fix it or build it, and make the code better for everyone. I'm really just pitching open source libraries here, however in any commercial context, building your own email renderer without a big team, is a bad idea.
As Marc B stated, I believe an IFrame would be your best bet... but please realize that if you just dump any email HTML code you risk exposing yourself to viruses, Trojans, and malicious HTML/JavaScript code - Your opening Pandora's box on your computer unless you find a good way to sandbox/strip that HTML.
Here's a simple Regex to clean JavaScript at least :
"(?s)<script.*?(/>|</script>)"
Consider the use of some HTML Tidy library (i.e.: PHP.Tidy).
You can pass the text through the library to get well formatted html.
A good practice would be to define a CSS standard behaviour for most tags in the div you're using.
Create a DIV container that you assign width (and height if needed) to, and make sure you add an overflow property to match your design. This should keep your email HTML from interfering with your layout.
UPDATE
A DIV container still assures you that you can constrain the size of the display box and with appropriate CSS acts similar to an iframe without all the baggage.
If you are worried about the code in the email, strip_tags would seem a better solution than the regex. You can define a list of tags to leave alone and still be confident of stripping the rest.

Categories