将文件附加到PHPMailer

时间:2014-10-02 08:15:47

标签: php file email attachment phpmailer

如何将文件附加到邮件?

<?php

    /* Contact Form Setup Begin */

    $send_name      = "SSSS";                       // Replace your name
    $send_title     = "New Application";            // Replace email sent title
    $send_address   = "some.user@gmail.com";    // Replace your email address

    $smtp_address   = "info@somesite.org";      // Replace your email address
    $smtp_password  = "Test12345!@#$%";                     // Replace your email password
    $smtp_server    = "mail.somesite.org";      // Replace your email server address

    /* Contact Form Setup End */

    date_default_timezone_set('Etc/UTC');

    require 'inc/phpmailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();                            // Create a new PHPMailer instance

    $mail->IsSMTP();                                    // Tell PHPMailer to use SMTP
    $mail->SMTPAuth = true;

    $mail->CharSet = "utf-8";                           // Set CharSet
    $mail->Host = $smtp_server;                         // Set the hostname of the mail server
    $mail->Port = 587;                                  // Set the SMTP port number - likely to be 25, 465 or 587
//  $mail->SMTPSecure = "tls";                          // If you use gmail address, active this line
    $mail->Username = $smtp_address;                    // Username to use for SMTP authentication
    $mail->Password = $smtp_password;                   // Password to use for SMTP authentication

    $mail->setFrom( $mail->Username, $send_title );     // Set who the message is to be sent from
    $mail->addAddress( $send_address, $send_name );     // Set who the message is to be sent to
    $mail->Subject = $send_title;                       // Set the subject line

    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML("Full name: ".$_POST["name"]." ".$_POST["surname"]."<br />Date of birth: ".$_POST["dob"]."<br />E-mail: ".$_POST["email"]."<br />Phone: ".$_POST["phone"]."<br />University: ".$_POST["ua"]."<br />Faculty: ".$_POST["fd"]."<br />Graduation: ".$_POST["eyog"]."<br />Work: ".$_POST["we"]."<br />Why: ".$_POST["why"]."<br />CV Stored: Upload/".$_FILES["file"]["name"]);

    $allowedExts = array("doc", "docx", "pdf");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    if (($_FILES["file"]["size"] < 10240000) && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/" . $_FILES["file"]["name"]);
            echo "Application sent";
        }
    } else {
        echo "Invalid file";
    }

    //send the message, check for errors
    if (!$mail->send()) { echo "."; } else { echo "!"; }
?>

1 个答案:

答案 0 :(得分:1)

您可以将AddAttachment用于此目的。

找到一个基本示例here