如何使用mail()发送附件


How to send an attachment with mail()

我想用电子邮件发送附件,可以吗?我的客户已经在谷歌上注册了电子邮件。这就是我目前所拥有的:

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "'r'n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>

您可以使用PHPMailer php库发送电子邮件
http://phpmailer.worxware.com/

        <?php 
        require "[YOUR PATH TO PHPMAILER LIB]/class.phpmailer.php";
        require "[YOUR PATH TO PHPMAILER LIB]/class.smtp.php";
    $mail = new PHPMailer;
            $mail->isSMTP(); // Set mailer to use SMTP
            $mail->Host = 'mtp.host.com'; // Specify main SMTP servers
            $mail->SMTPAuth = true; // Enable SMTP authentication
            $mail->Username = '[email address to send email]'; // SMTP username
            $mail->Password = '[above email password]'; // SMTP password
            $mail->SMTPSecure = 'encryption type'; // Enable TLS encryption, `ssl` also accepted
            $mail->Port = [SMTP PORT]; // TCP port to connect to
            $mail->From = '[FROM EMAIL]';
            $mail->FromName = '[FROM NAME]';
            $mail->addAddress([recipient email address], [recipient name]);    // Add a recipient
            $mail->addReplyTo(reply@example.com, reply);
            $mail->addAttachment([attachment path], [attachment name]);// name
            $mail->isHTML(true); // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $body;
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
            $mail->send();
?>

使用PHPMailer类,

 include 'PHPMailer.php';
    $email = new PHPMailer();
    $email->From      = 'you@example.com';
    $email->FromName  = 'Your Name';
    $email->Subject   = 'Message Subject';
    $email->Body      = $bodytext;
    $email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();

下载PHPMailer类,

https://github.com/PHPMailer/PHPMailer