Page 1 of 1

How to make links hot in HTML email?

Posted: Wed Apr 23, 2008 8:11 am
by jswen
Hello Chris and Swift Mailer forum users!

I have made a CMS system with a registration / login system. After a user registers to use it, an email is sent to the user with an activation code attached to the URL. The user is to click on this link in the activation email which will take them to an activation page on my sight. The email and activation code work fine.

However, the link in the email is not hot. So, when a person receives the email they have to copy the link and paste it into the URL window of their browser. Then, they can come to the activation page of my site.

Unfortunately, after spending days trying to figure out how to make this link hot, I still cannot do it. To be honest, I installed Swift Mailer because I thought it would allow me to make links that are hot. I am relatively new to PHP so I am probably missing something obvious. But, at least Swift Mailer does work for me. :)

Please advise on how to make the link being sent a hot link. Below is the code I used to send the link through Swift Mailer: (Note: The code works fine. It just does not make a hot link.

<?php
// Call the swift mail connections.
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

// Connect to swiftmail.
$smtp =& new Swift_Connection_SMTP("blabla.com", 25);
$smtp->setUsername("bla@blabla.com");
$smtp->setpassword("XXXXXXX");

$swift =& new Swift($smtp);

// Create the message.
$message =& new Swift_Message("Report", "<html><body>Hi XXX,<br /> It has been a long time since we have talked. Please visit my site and let me know how you are doing. :) <br />
<a href=\"www.blabla.com\">Come to my site.</a> &nbsp;<u>Cool</u></body></html>", "text/html");

if ($swift->send($message, "bla@blabla.com", "ttt@jomoeo.com"))
{
echo "Message sent";
}
else
{
echo "Message failed to send";
}

$swift->disconnect();
?>


Any advice is much appreciated !

Cheers,

Re: How to make links hot in HTML email?

Posted: Wed Apr 23, 2008 12:25 pm
by jswen
After I made my post, I soon found a solution using Swift Mailer. :)
Below is the code that worked :) Interestingly, I found the solution on a PHPmailer tutorial which swift mailer is supposed to be replacing.

<?php // An HTML email through Swiftmailer (http://www.swiftmailer.org).

/************ Set up the HTML body. *******************/

// Below the $htmlbody variable contains all the HTML content of my email.

$htmlbody = '<html>
<head>
<title>My HTML Email</title>
</head>
<body>
<h2>Test HTML email !!</h2>
<p>We invite you to visit <a href="[www.mysite.com]; title="mysite">mysite.com</a> Learn PHP it is awesome! </p>

<p>Sincerely,<br />
Your name</p>';

/*********** Above is all the HTML content *************/


/*********** Tell Swiftmailer to connect to the mail server using SMTP ************/

// Call the swift mail SMTP connection settings.

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

// Connect to swiftmail -- settings are provided by webhost.
$smtp =& new Swift_Connection_SMTP("mysite.com", 25);
$smtp->setUsername("me@mysite.com");
$smtp->setpassword("XXXXX");

$swift =& new Swift($smtp);

/************ End of the SMTP connection commands **********/


/********** Send the message using Swiftmailer *******************/

// Create the message.
// Note: The $htmlbody variable made above is put here.

$message =& new Swift_Message("Report", "$htmlbody", "text/html");

// Send it and test if successful.

if ($swift->send($message, "sendto@myfriend.net", "me@mysite.com"))
{
echo "Message sent";
}
else
{
echo "Message failed to send";
}

$swift->disconnect();

?>

This worked for me. I tested it and the result was a nice HTML email and most importantly the links were hot. :)

I hope this is helpful. I have spent over a week trying to send HTML emails through PHP and I finally just got one to work.

Re: How to make links hot in HTML email?

Posted: Thu Apr 24, 2008 4:13 am
by jswen
My previous script above worked but was not modeled after the tutorials at Swiftmailer.com. After spending some time studying the tutorials, this is the script I came up with. I printed them out and read them before. But, it was not until the second or third reading that I got things working.

Code: Select all

<?php 
/*****************
 
        This is a sample HTML email set up to be sent through Swiftmailer. 
        The HTML email has an embedded graphic and hot link set inside a table.  
        This is a modification of the script located in the Docs section of swiftmailers' website.  
        http://www.swiftmailer.org/wikidocs/v3/ ... ing_images.
        I tested the script and it works.       
 
        Hopefully, this will help people just starting with HTML email and swiftmailer.  
 
******************/ 
 
// Call the swiftmailer scripts.
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
// Set settings to connect to the mail server through the SMTP protocal
// Note: The username and password are on servers that require authentication.
$smtp =& new Swift_Connection_SMTP("yayaya.com", 25);
$smtp->setUsername("yaper@yayaya.com");
$smtp->setpassword("xxxxxx");
 
$swift =& new Swift($smtp);
 
$message =& new Swift_Message("Hello from me");
 
//  Embed an image within a table and make the link hot.
$message->attach(new Swift_Message_Part("<html><body><p><center>
 
<table width=\"50%\">
 
<tr>
<td><center>
This email contains this image:<br />
<img src=\"" . $message->attach(new Swift_Message_Image(new Swift_File("images/name.png"))) . "\" /><br />
which is embedded
</center></td>
</tr>
 
<tr>
<td><center>
Come visit my website !! <a href=\"http://www.yayaya.com\">Click here</a>
</center></td>
</tr>
 
</table></center></p></body></html>", "text/html"));
 
if ($swift->send($message, new Swift_Address("yaya@yayay.net", "Name"), new Swift_Address("admin@company.com", "Administor's Name")))
{
    echo "Message sent";
}
else
{
    echo "Sending failed";
}
 
//recommended to do this
$swift->disconnect();
?>