繁体   English   中英

为什么此php代码不发送电子邮件?

[英]Why does this php code not send email?

我在命令行上的UNIX共享托管帐户上执行它,但是它不发送任何电子邮件。 这有什么问题? 我的代码来自: PHP:如何使用smtp设置发送带有附件的电子邮件? 但仍然无法正常工作。

<?php
include('Mail.php');
include('Mail/mime.php');


// include_once('Mail/mime.php');|

// The code below composes and sends the email|

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './a.php';
$crlf = "\r\n";
$hdrs = array("From"=>'contactus@site.com', "Subject"=>"hello" ,"Reply-To"=>"contactus@site.com");

$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('rag.7raggupta@gmail.com', $hdrs, $body);

您是否尝试过mail($to, $subj, $body) 服务器设置可能有问题,而Pear软件包或PHP本身不一定有问题。

首先,您是否安装了Pear和Mime_Mail软件包? 如果您不这样做,那么它将不起作用,因为它是梨特定的代码。

接下来,假设安装了pear,我将发布上面版本的代码,我认为它将照常运行。

<?php

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

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './a.php';
$crlf = "\n";
$hdrs = array(
              'From'    => 'contactus@site.com',
              'Subject' => 'Hello',
              'Reply-To' => 'contactus@site.com'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'application/x-httpd-php');

// do not ever try to call these lines in reverse order
// when using versions older than 1.6.0
$body = $mime->get();
// or in 1.6.0 and newer
// $body = $mime->getMessageBody();

$hdrs = $mime->txtHeaders($hdrs);

$mail =& Mail::factory('mail');
$mail->send('rag.7raggupta@gmail.com', $hdrs, $body);

?>

我不确定您是否要以八位字节流的形式发送php附件,因为我认为这不是php脚本的适当标识。 我将其修改为php的正确mime类型。

有关更多参考,请查看此Mail_Mime手册的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM