Search code examples
phpgmailmime

PHP Mail Function - Add external image


I'm trying to add an image stored on my server to the email sent with PHP's mail() function. Here's the php code below:

$subject = 'Password Reset';

$message = '
<html>
<head>
    <title>Password Reset</title>
</head>
<body>
    Salve '.$username.', utilizza questo link per resettare la password: <a href="https://gestionale.boscarato.eu/Elaborato/reset/reset_password.php?c='.$operation_code.'">Resetta la tua password </a>
    <br><br>
    <img src="http://gestionale.boscarato.eu/Elaborato/images/logo.png" alt="Logo" width="300" height="50">
    <br>
    Cordiali saluti,
    <br>
    <h5>Luca Boscarato [5E] </h5>
</body>
</html>
';

$headers =  'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'From: Luca Boscarato <boscarato.luca@gmail.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

mail($email, $subject, $message, $headers);

The problem is that the image can't be shown (I've already activated the 'accept-all' option on gmail so that my client accepts external images too.


Solution

  • Replace the http:// protocol (in the image URL) to https://.

    The error is caused by incorrect configuration of the remote server.

    HTTPS loads correctly

    https://gestionale.boscarato.eu/Elaborato/images/logo.png  # HTTPS correct
    

    But HTTP redirects to invalid URL

    http://gestionale.boscarato.eu/Elaborato/images/logo.png  # HTTP before redirect
    

    redirects to

    https://gestionale.boscarato.euelaborato/images/logo.png  # invalid URL
    

    Mind the missing slash after the .eu TLD.

    curl -I "http://gestionale.boscarato.eu/Elaborato/images/logo.png"
    HTTP/1.1 301 Moved Permanently
    Date: Fri, 14 May 2021 19:21:06 GMT
    Server: Apache/2.4.38 (Raspbian)
    Location: https://gestionale.boscarato.euElaborato/images/logo.png
    Content-Type: text/html; charset=iso-8859-1