0

I am trying to use the mail function to send an email but can't seem to get my link to work. It is displaying as a string instead of a link.. I am not sure how to closed off the string quotation or what the correct format is....

$company = 'pianocourse101@hotmail.com';
   $subject = 'Account temporary suspended due to failed login attempts';
   $mailTo = $row['user_email'];
   $headers = 'From: '.$company;
   $txt = "Hello ".$row['user_first']."" .$row['user_last']."! \n\n Your account has been temporary suspended because you or someone claiming to be you has failed to login into their account more than on more than five occasions. \n\n Please click on the following link to change your password so that you can login again <a href='reset.php'>Click here to reset your password </a>";

    mail($mailTo, $subject, $txt, $headers);
  • 1
    Possible duplicate of [Send HTML email with php?](https://stackoverflow.com/questions/12941479/send-html-email-with-php) – Nico Haase Oct 09 '18 at 07:25
  • I did try to have a look at that thread but still am confused –  Oct 09 '18 at 07:33
  • 1
    you can just set www.yourdomain.com/reset.php as the link, i.e.: `$txt = "Hello ".$row['user_first'] . $row['user_last']. "! \n\n Your account has been... Please click link to login again: www.yourdomain.com/reset.php";` – lovelace Oct 09 '18 at 07:42
  • If you want to fasten your development, don't write stuff around `mail`, but rather use libraries like PHPMailer, SwiftMailer,... – Nico Haase Oct 09 '18 at 08:23
  • thanks but I don't have a domain yet –  Oct 09 '18 at 08:31
  • Have you tried my answer in bottom? Because i tested that to my gmail. – sukalogika Oct 09 '18 at 08:45
  • Yup.. I can't get it to work with my hotmail... but will try gmail –  Oct 09 '18 at 09:06
  • Does this answer your question? [Sending HTML email with PHP](https://stackoverflow.com/questions/10031380/sending-html-email-with-php) – Nico Haase Mar 07 '20 at 13:07

2 Answers2

1

It looks like your mail is not in HTML format. I suggest you start using PHPMailer: https://github.com/PHPMailer/PHPMailer It's really easy to use and In PHPMailer you can send HTML emails!

Here is their simple example:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

To install PHPMailer you can use composer. After installing composer you can install PHPMailer with composer like this:

composer require phpmailer/phpmailer

or download the files manually

0

This will help your links work, as tag is html tag so mime type should be define html so it instruct that email will sent in html format.

$company = 'pianocourse101@hotmail.com';
$subject = 'Account temporary suspended due to failed login attempts';
$mailTo = $row['user_email'];

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$company;
$txt = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                            <html xmlns="http://www.w3.org/1999/xhtml">
                                <head>
                                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                                </head>

                                <body>
                                    <div>';

$txt .= 'Hello '.$row['user_first'].' '.$row['user_last'].'!<br><br>Your account has been temporary suspended because you or someone claiming to be you has failed to login into their account more than on more than five occasions. <br><br> Please click on the following link to change your password so that you can login again <a href="reset.php">Click here to reset your password </a>';

$txt.='                             </div>
                                </body>
                            </html>';

    mail($mailTo, $subject, $txt, $headers);

Hope it will help you.

Kamran Allana
  • 543
  • 1
  • 6
  • 25
  • Hello and thanks for the assistance here but the link looks like it doesn't work on my pc because it looks like this but when I did open it with my mobile phone... it does work –  Oct 09 '18 at 08:13
  • I hope that the picture was uploaded successfully –  Oct 09 '18 at 08:16
  • I can actually copy and paste the email here of what it says –  Oct 09 '18 at 08:16
  • Hello Mervin Lee! Your account has been temporary suspended because you or someone claiming to be you has failed to login into their account more than on more than five occasions. Please click on the following link to change your password so that you can login again [reset.php]Click here to reset your password –  Oct 09 '18 at 08:16
  • Would you please share the picture. – Kamran Allana Oct 09 '18 at 14:44
  • I got it working guys.... just didn't realise that you can just add the plain url as in http://localhost...... etc –  Oct 10 '18 at 05:38