HiDayurie Dave
HiDayurie Dave

Reputation: 1807

PHPMailer can't send if there is a link href on IsHTML(true)

I am using PHPMailer to send email.

$mail = new PHPMailer();
$mail->SMTPAuth= true;
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.live.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->From = "[email protected]";
$mail->FromName = "Office";
$mail->addAddress($getEmailAddress); 
$mail->IsHTML(true);

$mail->Subject = "SIIX Email Renewal verification";
$mail->Body    = "
    <div style='line-height: 20px;'>
        Dear All Outlook email user,<br><br>

        To complete the outlook email renewal this year we need to verify whether this email received successfully or not.<br>
        Please kindly help to verify by open the link below to confirm it.<br><br>

        <a href='//1.1.1.1:8082/renewal/mail/verify.php?email=$getEmailAddress' style='font-size: 20px; color: #0055a7; font-weight: bold;'>
            <img src='http://1.1.1.1:8082/renewal/mail/outlook.png'/><br>

        <u>Verify Now</u></a><br><br>

        Thank you<br>
        Outlook Exchange Mail Administrator<br>

        <img src='http://1.1.1.1:8082/renewal/mail/office.jpg'/>
    </div>
";
$mail->Send();

When I try to run the code, on email receive I can't click Verify Now on mobile phone.

<a href='//1.1.1.1:8082/renewal/mail/verify.php?email=$getEmailAddress' style='font-size: 20px; color: #0055a7; font-weight: bold;'>
            <img src='http://1.1.1.1:8082/renewal/mail/outlook.png'/><br>

        <u>Verify Now</u></a>

Then I try to modify it to be (add http:)

<a href='http://1.1.1.1:8082/renewal/mail/verify.php?email=$getEmailAddress' style='font-size: 20px; color: #0055a7; font-weight: bold;'>
            <img src='http://1.1.1.1:8082/renewal/mail/outlook.png'/><br>

        <u>Verify Now</u></a>

When try to run the code, I didn't see any email receive.

Then I try to modify it again (Add \):

<a href=\'http://1.1.1.1:8082/renewal/mail/verify.php?email=$getEmailAddress\' style='font-size: 20px; color: #0055a7; font-weight: bold;'>
            <img src='http://1.1.1.1:8082/renewal/mail/outlook.png'/><br>

        <u>Verify Now</u></a>

Then I try to run the code again and got the result of the link is:

http://\http://1.1.1.1:8082/renewal/mail/[email protected]\

How to fix that?

Update

Log:

SMTP -> FROM SERVER:220 SG2PR04CA0156.outlook.office365.com Microsoft ESMTP 

MAIL Service ready at Wed, 16 May 2018 09:09:58 +0000 
SMTP -> FROM SERVER: 250-SG2PR04CA0156.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:220 2.0.0 SMTP server ready 
SMTP -> FROM SERVER: 250-SG2PR04CA0156.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:250 2.1.0 Sender OK 
SMTP -> FROM SERVER:250 2.1.5 Recipient OK 
SMTP -> FROM SERVER:354 Start mail input; end with . 
SMTP -> FROM SERVER:250 2.0.0 OK [Hostname=SG2PR06MB2122.apcprd06.prod.outlook.com] 
SMTP -> FROM SERVER:220 SG2PR04CA0152.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 16 May 2018 09:09:59 +0000 
SMTP -> FROM SERVER: 250-SG2PR04CA0152.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:220 2.0.0 SMTP server ready 
SMTP -> FROM SERVER: 250-SG2PR04CA0152.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:250 2.1.0 Sender OK 
SMTP -> FROM SERVER:250 2.1.5 Recipient OK 
SMTP -> FROM SERVER:354 Start mail input; end with . 
SMTP -> FROM SERVER:250 2.0.0 OK [Hostname=SG2PR06MB2122.apcprd06.prod.outlook.com] 
SMTP -> FROM SERVER:220 SG2PR04CA0167.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 16 May 2018 09:10:00 +0000 
SMTP -> FROM SERVER: 250-SG2PR04CA0167.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:220 2.0.0 SMTP server ready 
SMTP -> FROM SERVER: 250-SG2PR04CA0167.outlook.office365.com Hello [175.111.116.70] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 
SMTP -> FROM SERVER:250 2.1.0 Sender OK 
SMTP -> FROM SERVER:250 2.1.5 Recipient OK 
SMTP -> FROM SERVER:354 Start mail input; end with . 
SMTP -> FROM SERVER:250 2.0.0 OK [Hostname=SG2PR06MB2122.apcprd06.prod.outlook.com] 

Upvotes: 1

Views: 1498

Answers (1)

Synchro
Synchro

Reputation: 37810

Protocol-relative URLs (starting with //) can't work in email because they have no protocol to be relative to - generally speaking, an email message does not have a URL. See my answer on this subject.

Don't add slashes; that won't help. I'd recommend switching to a heredoc for setting your message body - it avoids quoting problems altogether, and variable interpolation will still work. For example:

$mail->Body = <<<EOT
    <div style="line-height: 20px;">
        Dear All Outlook email user,<br><br>

        To complete the outlook email renewal this year we need to verify whether this email received successfully or not.<br>
        Please kindly help to verify by open the link below to confirm it.<br><br>

        <a href="http://1.1.1.1:8082/renewal/mail/verify.php?email=$getEmailAddress" style="font-size: 20px; color: #0055a7; font-weight: bold;">
            <img src="http://1.1.1.1:8082/renewal/mail/outlook.png"/><br>

        <u>Verify Now</u></a><br><br>

        Thank you<br>
        Outlook Exchange Mail Administrator<br>

        <img src="http://1.1.1.1:8082/renewal/mail/office.jpg"/>
    </div>
EOT;

One other observation - the message body should be a complete HTML document, including html, header, & body tags - you have just a fragment.

When you say "I didn't see any email receive" do you mean that sending the email failed? You have no error checking in your code, so you're not going to see errors if they occur. Base your code on the examples provided with PHPMailer, which do include basic error checks.

Upvotes: 2

Related Questions