代码之家  ›  专栏  ›  技术社区  ›  Naveed

PHP:如何使用smtp设置发送带有附件的电子邮件?

  •  8
  • Naveed  · 技术社区  · 14 年前

    require_once "Mail.php";
    
    $from = "Usman <from@example.com>";
    $to = "Naveed <to@example.com>";
    $subject = "subject";
    $body = "";
    
    $host = "smtp.gmail.com";
    $username = "username";
    $password = "password";
    
    $headers = array ('From' => $from,
          'To' => $to,
          'Subject' => $subject);
    
    $smtp = Mail::factory( 'smtp', array('host' => $host,
              'auth' => true,
              'username' => $username,
              'password' => $password ) );
    
    $mail = $smtp->send( $to, $headers, $body );
    
    if ( PEAR::isError($mail) ) {
    echo( "<p>" . $mail->getMessage() . "</p>" );
    } else {
    echo( "<p>Message successfully sent!</p>" );
    }
    
    5 回复  |  直到 14 年前
        1
  •  11
  •   vinnyjames    5 年前

    google://pear mail attachment 搜索。

    include('Mail.php');
    include('Mail/mime.php');
    
    $text = 'Text version of email';
    $html = '<html><body>HTML version of email</body></html>';
    $file = './files/example.zip';
    $hdrs = array(
                  'From'    => 'someone@domain.pl',
                  'To'      => 'someone@domain.pl',
                  'Subject' => 'Test mime message'
                  );
    
    $mime = new Mail_mime();
    
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    
    $mime->addAttachment($file,'application/octet-stream');
    
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
    
    $mail =& Mail::factory('mail', $params);
    $mail->send('mail@domain.pl', $hdrs, $body); 
    
        2
  •  3
  •   apiri    14 年前

    如果您另外使用PHP PEAR Mail_Mime 模块它提供了适当的处理和编码,将附件作为电子邮件的一部分。

        3
  •  2
  •   swisswiss    6 年前

    <?php
    require_once "Mail.php"; // PEAR Mail package
    require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
    
    $from = "Robert Davis <robertdavis@pobox.com>";
    $to = "Sam Hill <sam.hill@aol.com>";
    $subject = 'Test mime message with an attachment';
    
    $headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
    
    $text = 'Text version of email';// text and html versions of email.
    $html = '<html><body>HTML version of email. <strong>This should be bold</strong></body>        </html>';
    
    $file = './sample.txt'; // attachment
    $crlf = "\n";
    
    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html);
    $mime->addAttachment($file, 'text/plain');
    
    //do not ever try to call these lines in reverse order
    $body = $mime->get();
    $headers = $mime->headers($headers);
    
    $host = "sasl.smtp.pobox.com";
    $username = "robertdavis@pobox.com";
    $password = "Kdu48Adi3";
    
    $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
     'username' => $username,'password' => $password));
    
    $mail = $smtp->send($to, $headers, $body);
    
    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
    }
    else {
      echo("<p>Message successfully sent!</p>");
    }
    ?>
    
        4
  •  1
  •   TheHippo    14 年前

    用PHP发送电子邮件总是让人觉得有点挣扎。如果您能够使用它们,我建议您选择以下两种PHP邮件库之一:

        5
  •  0
  •   Gazillion    14 年前

    看看Mail\u Mine对象,它完成了您要做的事情,并且具有添加附件的简单方法(简单地称为addAttachments)。

    http://pear.php.net/manual/en/package.mail.mail-mime.php