PHPMailer / PHPMailer

The classic email sending library for PHP
GNU Lesser General Public License v2.1
21.06k stars 9.74k forks source link

Image can't be displayed when sending with PHPMailer #1445

Closed shiluda closed 6 years ago

shiluda commented 6 years ago

the recipient receives the email but cannot view the image attached

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

if(isset($_POST['send'])){

require 'C:/xampp/htdocs/sniper/PHPMailer/src/Exception.php';
require 'C:/xampp/htdocs/sniper/PHPMailer/src/PHPMailer.php';
require 'C:/xampp/htdocs/sniper/PHPMailer/src/SMTP.php';

$mail = new PHPMailer;
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
//Enable SMTP debugging. 
$mail->SMTPDebug = 4;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "ssl://smtp.gmail.com";
//$mail->Host = "tls://smtp.mail.yahoo.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;  
$mail->SMTPAutoTLS = false;
//Provide username and password     
$mail->Username = "email@gmail.com";                 
$mail->Password = "password";  

//If SMTP requires TLS encryption then set it ssl
$mail->SMTPSecure = "ssl";                           
//Set TCP port to connect to 
$mail->Port = 465;  

$mail->From ="email@gmail.com";
$mail->FromName = "user1";

$mail->addAddress("email2@gmail.com", "user2");

//Provide file path and name of the attachments

$mail->addStringAttachment(file_get_contents($url), 'img.png');
$url = "https://www.pexels.com/photo/woman-wearing-black-crew-neck-shirt-and-blue-denim-shorts-911575/";

$mail->isHTML(true);

$mail->Subject = "Registration Form";
$mail->Body = "<i>Kindly find attached the Registration Form below.</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
} 

}

?>
Synchro commented 6 years ago

You need to apply some basic common sense here. Just look at your code:

$mail->addStringAttachment(file_get_contents($url), 'img.png');
$url = "https://www.pexels.com/photo/woman-wearing-black-crew-neck-shirt-and-blue-denim-shorts-911575/";
  1. You're setting the URL after you're trying to use it.
  2. The URL does not link to an image.

Doing this interactively is a bad idea anyway - it's very slow.

shiluda commented 6 years ago

Thanks it's working!