Sending a simple attached file via PHP mail() function

前端 未结 1 1317
感情败类
感情败类 2021-02-11 10:25

I\'m going to give this another try because my last question might have been confusing. I have a simple web form consisting of the following some inputs (for no

1条回答
  •  灰色年华
    2021-02-11 10:52

    It is possible to do with all 3 libraries you listed (PHPMAiler, PEAR and Swiftmailer).

    For PHPMailer you can see a tutorial here:

    require_once '../class.phpmailer.php';
    
    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
    
    try {
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
      $mail->SetFrom('name@yourdomain.com', 'First Last');
      $mail->AddReplyTo('name@yourdomain.com', 'First Last');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML(file_get_contents('contents.html'));
      $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
      $mail->Send();
      echo "Message Sent OK

    \n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! }

    AddAttachment will take a file from your server.

    How to upload a file form an HTML form can be found here. Once your email is sent you can delete (unlink) the file from the server.

    The PHP manual can help you to better undersand file uploads.

    All you want to do is easy to achieve, but it's longer to explain than do it :) But with all the links I gave you you have everything you need. If you have specific questions let me know.

    0 讨论(0)
提交回复
热议问题