发送带有图像的电子邮件的正确设置是什么?
我为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);
这不会发送邮件和附件。有人能帮我吗?谢谢!
答案 0 :(得分:1)
更改此:
$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')
作为参数直接传递,因为该函数将多个变量作为参数。