Question

I am new to Swift Mailer and would like to include a link in the body of the HTML message linking back to my home page however, the PHP script does not run when I include the anchor tag in the message (the script does work when I include a different element, such as a heading tag. Not sure what I might be missing.

PHP:

 <?php require_once '../Swift/lib/swift_required.php';

    // Create the Transport
    $transport = Swift_SmtpTransport::newInstance()
    ->setUsername('admin@mazihealth.com')
    ->setPassword('adminxxxx');

    // Create the Mailer using your created Transport
      $mailer = Swift_Mailer::newInstance($transport);
      $message = Swift_Message::newInstance();
      $message->setSubject('My subject');
      $message->setFrom('abc@mazihealth.com');
      $message->setTo('abc3@gmail.com');
      $cid = $message->embed(Swift_Image::fromPath('images/MaziFinal_email.jpg'));
      $message->setBody(
      '<html>' .
      ' <head></head>' .
      ' <body>' .
      ' This is my message' .
      '<a href="'http://www.mazihealth.com'">Sign in</a>'.
      ' </body>' .
      '</html>',
      'text/html' // Mark the content-type as HTML
      );

    // Send the message
      $result = $mailer->send($message);

      echo $message->toString();

      ?>
Was it helpful?

Solution

You have a syntax error. Just remove the single quotes in the href:

$message->setBody(
    '<html>' .
    ' <head></head>' .
    ' <body>' .
    ' This is my message' .
    '<a href="http://www.mazihealth.com">Sign in</a>'.
    ' </body>' .
    '</html>',
    'text/html' // Mark the content-type as HTML
);

Additionally, you might find it easier to use heredoc statements to outline your content. It makes reading/editing large strings easier. Here is an example:

$body = <<<EOD
<html>
    <head></head>
    <body>
        This is my message .
        <a href="http://www.mazihealth.com">Sign in</a>
    </body>
</html>
EOD;

$message->setBody(
    $body,
    'text/html' // Mark the content-type as HTML
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top