Page 1 of 1

How to "Forward to a Friend" an HTML email

Posted: Tue Jul 14, 2009 6:10 am
by iceangel
Hi everyone,

I have created an HTML email for a client, that must have on it, a "Send to a Friend" link. When clicked it should take the user to a basic form where the friend's email address can be input. On submission of this form, the HTML email that was received, will be forwarded to the friend.

I've been told one can do this in PHP, however i am not very clued up on PHP and i would really appreciated any help that you can give.

Thanks :)



Re: How to "Forward to a Friend" an HTML email

Posted: Tue Jul 14, 2009 7:09 am
by turbolemon
Hi,

You just need to use the php mailer functionality. I use php frameworks, my preferred is Zend Framework. You can use the Zend_Mail class standalone, using just a few lines of code:

mailer.php

Code: Select all

 
<?php
require_once('Zend/Mail.php');
 
$mail = new Zend_Mail();
    
$mail->setBodyText("Your message");
$mail->setBodyHtml("Your HTML message");
$mail->setFrom('you@youraddress.co.uk', strip_tags($_POST['name']));
$mail->addTo(strip_tags($_POST['mail']), strip_tags($_POST['name']));
$mail->setSubject("Email title");
$mail->send();
 


The code implies that the ZF library folder is in your include path, or relative to the running script (not wise to have it publically accessible, though). You should also validate and filter the input from the form before sending it out, and have some safeguards to prevent spamming and robots. You need the HTML form to submit to the above script like this:

send.htm

Code: Select all

 
<form name="form1" method="post" action="mailer.php">
Your name: <input type="text" name="name" id="name" /><br />
Friends email: <input type="text" name="mail" id="mail" />
</form>
 
It's a really simple example, and that HTML isn't very semantic, but it should do the job.

Hope that was helpful.

Re: How to "Forward to a Friend" an HTML email

Posted: Wed Jul 15, 2009 6:12 am
by turbolemon
Okay, i'll go into the set-up a little further:

Download the current version of the framework http://framework.zend.com/releases/Zend ... inimal.zip, extract the archive, and upload the Zend folder (from within the library sub-folder) to your web server. Either upload it on your include path (check this using phpinfo() command in a php script), or relatively to the mailer.php file from my previous comment. If it's located in a public-facing folder, throw in a .htaccess file or modify your httpd.conf to block access to the files contained (assuming you are using Apache web server).

I don't know how you would determine the HTML email to send, it really depends on your current implementation. You could use a get variable for this from the email hyperlink (e.g. http://mysite.com/sendfriend.php?i[b]d=7[/b]), storing the email layout in a flat file or database table on the web server. You could even have substitution variables so that the email is personalized for the recipient, using sprintf http://uk.php.net/sprintf (formatted strings).

There are lots of ways of facilitating the functionality you are looking for.