PHP:如何使用smtp设置发送带附件的电子邮件? [英] PHP: How to send email with attachment using smtp settings?

查看:475
本文介绍了PHP:如何使用smtp设置发送带附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码成功发送电子邮件。但是现在我想附一封电子邮件的文本文件(例如:test.txt)。任何想法?

  require_onceMail.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>);
}


解决方案

c code code

 包括( 'Mail.php'); 
include('Mail / mime.php');

$ text ='电子邮件的文本版本';
$ html ='< html>< body>电子邮件的HTML版本< / body>< / html>';
$ file ='./files/example.zip';
$ crlf =rn;
$ hdrs = array(
'From'=>'someone@domain.pl',
'To'=>'someone@domain.pl',
'主题'=>'测试mime消息'
);

$ mime = new Mail_mime($ crlf);

$ mime-> setTXTBody($ text);
$ mime-> setHTMLBody($ html);

$ mime-> addAttachment($ file,'application / octet-stream');

$ body = $ mime-> get();
$ hdrs = $ mime->标头($ hdrs);

$ mail =& Mail :: factory('mail',$ params);
$ mail-> send(aail@domain.pl',$ hdrs,$ body);


I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?

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>" );
}

解决方案

Found this code as one of the first hits of the google://pear mail attachment search.

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';
$crlf = "rn";
$hdrs = array(
              'From'    => 'someone@domain.pl',
              'To'      => 'someone@domain.pl',
              'Subject' => 'Test mime message'
              );

$mime = new Mail_mime($crlf);

$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); 

这篇关于PHP:如何使用smtp设置发送带附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆