我正在通过PHP mail()函数发送带有附件的信件,它的工作过程中没有出现任何错误。消息直接发送给接收者,所有文本均保持原样。问题是,如果我将邮件发送到GMail地址,我发送的附件(PDF和图像)就可以了,但是如果我将邮件发送到其他邮件(我尝试使用Mail.ru和某些站点邮件),它们会损坏。如果我将邮件转发到GMail,它们仍然损坏,无法打开。但是,如果我将未损坏的邮件从GMail转发到其他邮件地址,则文件就可以了。如果消息完全相同,怎么可能?
我在使用mail()函数时遇到了同样的麻烦,我的建议是使用PHPMailer并通过SMTP发送电子邮件。
这是一个使用PHPmailer的小代码段:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpserver.com'; // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句