简体   繁体   English

使用phpmailer通过带有图像的html文件发送电子邮件

[英]Sending email through html file with images using phpmailer

I am sending emails to my subscribers using phpmailer. 我正在使用phpmailer向我的订阅者发送电子邮件。 I have an HTML file which contains text and images. 我有一个包含文本和图像的HTML文件。 Emails are sending successfully but the weird thing is my images are not showing. 电子邮件发送成功,但是奇怪的是我的图像没有显示。 My images are located at root directory of my HTML file. 我的图像位于我的HTML文件的根目录中。 Is there a way to send email through HTML file with images located at my local directory? 有没有一种方法可以通过HTML文件发送带有本地目录中图像的电子邮件? I put my images in my HTML file like <img src="images/log.png"> 我将图像放入<img src="images/log.png">

I don't want to give my image url in my HTML file like 我不想在我的HTML文件中提供图片网址,例如

<img src="www.mydomain.com/images/log.png">

here is my code: 这是我的代码:

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$link= mysqli_connect("$db_host","$db_username","$db_pass", "db") or die ("could not connect to mysql"); 
//if($link){echo "connect";}
// 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\Exception;

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


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
     $query = "select customer_email from subscribers";
     $result = mysqli_query($link, $query) or die("No customer in the table");;

     while($values = mysqli_fetch_array($result)){

         $toemail = $values['customer_email'];


    //Server settings
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = '(myhost)';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '(my email)';                 // SMTP username
    $mail->Password = '(my password)';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('haris.khan@premier.com.pk', 'Premier');
    $mail->addAddress(''.$toemail.'', ''.$name.'');     // Add a recipient
    $mail->addReplyTo('haris.khan@premier.com.pk', 'Information');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    ob_start();
include "file.html";
$contents = ob_get_clean();
$mail->msgHTML($contents);

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

here is my html file 这是我的html文件

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>

<body>
<img src="images/logo.png">
<h1> Hello World ..!!! </h1>
<p> This is the <b> auto generated email from HTML Fie </b> for testing purpose. </p>
</body>
</html>

any help would be highly appreciable. 任何帮助都是非常可观的。

I don't want to give my image url in my HTML file like... 我不想在HTML文件中提供图片网址,例如...

Well you're out of luck there. 好吧,你在那里不走运。 Email messages have no root URL, so relative URLs cannot work; 电子邮件没有根URL,因此相对URL无法正常工作。 you must include a hostname if you want to use linked images. 如果要使用链接的图像,则必须包含主机名。

Your only alternative is to use embedded images, where the image data is included within the message, though it's not something you should do for large volumes as it's very inefficient. 您唯一的选择是使用嵌入的图像,其中图像数据包含在消息中,但是由于效率很低,因此您不应该对大量数据执行此操作。 You can do this in PHPMailer using the addEmbeddedImage() method, and then refer to images from your HTML using their cid values. 您可以使用addEmbeddedImage()方法在PHPMailer中执行此操作,然后使用其cid值引用HTML中的图像。 For example: 例如:

$mail->addEmbeddedImage('images/logo.png', 'logo');

Then in your HTML: 然后在您的HTML中:

<img src="cid:logo">

The msgHTML() method does this for you. msgHTML()方法为您完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM