2012-12-16 19 views
-1

我正在使用PHPmailer(http://phpmailer.worxware.com/)類通過電子郵件發送表單信息。在窗體內部有一個像這樣的圖像:如何將圖像發送到電子郵箱?

<form.....> 

<div><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(42, 42); ?></div> 


</form> 

如何通過電子郵件發送該圖像?謝謝。

+0

請詳細說明。您的圖像是否在任何字段? – Arif

+0

不,它不在任何字段 – learn1208

回答

0

除非你必須通過你上傳圖片到服務器的文件輸入字段,您將無法通過的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等的一些東西等。

+0

如果圖像不是我該怎麼辦?它是由php生成的。 – learn1208

+0

我已經解釋說你有兩個選擇可以通過電子郵件向用戶發送圖片:附件和HTML郵件。對於附件,您需要通過字段將圖像上傳到您的服務器。對於HTML電子郵件,您只需要公開圖片即可。 – brezanac

相關問題