如何在phpMailer中发送xml(如附件)

时间:2016-07-21 08:43:20

标签: php phpmailer

    $myXML = 
        '<?xml version="1.0" encoding="ISO-8859-2"?>
        <Document-ORDRES>
            <ORDRES-Header>
                <OrderResponseNumber>123</OrderResponseNumber>
                <OrderResponseDate>0</OrderResponseDate>
                <BuyerOrderNumber>0</BuyerOrderNumber>
                <BuyerOrderDate>0</BuyerOrderDate>
                <DeliveryDate>0</DeliveryDate>
            </ORDRES-Header>
        </Document-ORDRES>';    

$xml=simplexml_load_string($myXML) or die("Error: Cannot create object");

public function send_mail($xml){
    print_r($xml);
    echo "</br></br>";
    $data;
    require 'C:\xampp\htdocs\PHPMailer-master\PHPMailerAutoload.php';

    header('Content-Type: text/html; charset=utf-8');
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 2;
    $mail->Debugoutput = 'html';
    $mail->Host = "...";
    $mail->Port = 587;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->SMTPAuth = true;

    $mail->Username = "...";
    $mail->Password = "...";
    $mail->setFrom('...', '...');
    $mail->addAddress('...', '');
    $mail->Subject = '...';
    $mail->AltBody = " ";
    $mail->msgHTML("Test");
    $mail->addAttachment($xml, "xml.xml");
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
}
你能告诉我该怎么办? 我想发送这个xml像附件,但在邮件中我没有附件。 我只收到了电子邮件文本。

1 个答案:

答案 0 :(得分:0)

使用addStringAttachment()方法代替addAttachment()

$mail->addStringAttachment($xml, "xml.xml");

addAttachment()添加了与文件路径的附件,因此您必须将xml保存为文件:

$file = __DIR__ . '/xml.xml';
file_put_contents($file, $myXML);

然后添加并删除它......

$mail->addAttachment($file);
$mail->send();
unlink($file);