使用 PHPmailer 发送带有 base64 图像附件的邮件

Sending mail with base64 image atttachment using PHPmailer

发送附有图片的电子邮件的正确设置是什么?

我为我的 include.php 创建了一个函数来获取所有详细信息以使用 phpmailer 发送:

function send_mail($args){  
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 0;
$mail->SMTPAuth   = true;
$mail->Port       = 25;
$mail->Host = "localhost";

$mail->Username = 'myusername';
$mail->Password = "*************";

$mail->From = "name@company.com";
$mail->FromName = "Notification"; 

$mail->Subject = $args['subject'];
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->AddStringAttachment($args['base64-attachment']);

$mail->MsgHTML($args['content']);

  if(strpos($args['to']['email'], ',')){
    $recipients = explode(',',$args['to']['email']);
    foreach($recipients as $v) $mail->AddAddress(trim($v), $args['to']['name']);  
  }else{
      $mail->AddAddress($args['to']['email'], $args['to']['name']);
  }

try {
  $mail->Send();
} catch (phpmailerException $e) {

} catch (Exception $e) {

}   
}

message.php 文件

    $imgdata = '...';//base64 image here
    $args['to']['name'] = 'John Smith';
    $args['to']['email'] = 'johnsmith@company.com';

    $args['content'] = 'Image attached!';
    $args['from']['email'] = 'mail@company.com';
    $args['from']['name'] = 'Company name';
    $args['subject'] = 'Image';

    $args['base64-attachment'] = ($imgdata, 'Contract.png', 'base64', 'image/png'); //error with this line

    send_mail($args);

这不会发送邮件和附件。有人能帮我吗?谢谢!

改变这个:

$args['base64-attachment'] = ($imgdata, 'Contract.png', 'base64', 'image/png');

为此:

$args['base64-image']      = $imgdata;
$args['base64-attachment'] = "Contract.png, base64, image/png";

我们要更新这个:

$mail->AddStringAttachment($args['base64-attachment']);

对此:

$attach_parameter = explode(",", $args['base64-attachment']);
$mail->AddStringAttachment($args['base64-image'], $attach_parameter[0], $attach_parameter[1], $attach_parameter[2]);

或者,如果它始终是 base64 图像,那么您可以这样做: 您也可以将 Contract.png 作为变量而不是

$mail->AddStringAttachment($args['base64-image'], 'Contract.png', 'base64', 'image/png');

您不能直接将此 ($imgdata, 'Contract.png', 'base64', 'image/png') 作为参数传递,因为该函数需要多个变量作为参数。