【发布时间】:2018-09-26 23:35:01
【问题描述】:
我正在使用 phpmailer 向我的订阅者发送电子邮件。我有一个包含文本和图像的 HTML 文件。电子邮件发送成功,但奇怪的是我的图像没有显示。我的图像位于我的 HTML 文件的根目录中。有没有办法通过 HTML 文件发送电子邮件,图像位于我的本地目录中?我把我的图片放在我的 HTML 文件中,比如
<img src="images/log.png">
我不想在我的 HTML 文件中提供我的图片网址,例如
<img src="www.mydomain.com/images/log.png">
这是我的代码:
$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;
}
这是我的 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>
任何帮助都会非常感激。
【问题讨论】:
-
使用图片的完整路径,例如
<img src="http://example.com/images/logo.png"> -
如果您不想通过 HTTP(S) URL 提供您的图像,那么您的另一个选择是将它们直接嵌入到电子邮件中 - 请参阅stackoverflow.com/questions/3708153/…(当然这会增加您的服务器必须为每封电子邮件发送的数据量。)
-
@CBroe 请再次查看我的问题。我编辑它。我在 https url 上有我的图像,但在图像标签中我不想将图像 src 提供为 https:domain.com/img。我将图像源作为 src="img/logo.png"
-
“但在图像标签中我不想将图像 src 作为 https:domain.com/img。我将图像源提供为 src="img/logo.png"" - 这显然行不通,因为电子邮件本身并未作为“来自您的域”的文档加载。它可以是完整的绝对路径,也可以是嵌入的图像。