除非你必須通過你上傳圖片到服務器的文件輸入字段,您將無法通過的PHPMailer發送它(或以其他任何方式進行那件事)。
<form>
...
<input type="file" />
...
</form>
這就是如果你真的想發送附加到電子郵件的圖像。另一方面,如果你想發送一封電子郵件,其中有圖像代碼嵌入到郵件正文本身,那麼我想你正在尋找一種發送HTML郵件的方式,這也是PHPMailer所支持的。下面是一個例子,你會如何做到這一點(注意圖像本身需要公開訪問)。
<?php
/**
* Sending an HTML email through PHPMailer and SMTP...
*/
require_once('PHPMailer.class.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->CharSet = 'utf-8';
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.example.com"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "[email protected]"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->AddReplyTo('[email protected]', 'Sending User');
$mail->AddAddress('[email protected]', 'Receiving User');
$mail->SetFrom('[email protected]', 'Sending User');
$mail->Subject = 'Image';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML('<html><body><img src="http://example.com/path_to_image.jpg" width="xxx" height="xxx" /></body></html>'));
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
至於HTML電子郵件,他們有自己的一套規則和最佳實踐。即如果您打算做比發送圖像更復雜的任何內容,您應該使用像這樣的CSS內聯,避免使用諸如background-image
等的一些東西等。
請詳細說明。您的圖像是否在任何字段? – Arif
不,它不在任何字段 – learn1208