Why does this php code not send email?
I'm executing it on unix shared hosting account on command line but it does not send any email. What is the problem in this? I've got the code from : PHP: How to send email with attachment using smt开发者_StackOverflow中文版p settings? but still it does not work.
<?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);
Have you tried mail($to, $subj, $body)
? It may be a problem with your server settings, and not necessarily with Pear package or PHP itself.
First, do you have Pear and the Mime_Mail package installed? If you don't then this will not work, as it is Pear specific code.
Next, assuming that pear is installed, I will post a version of the code above I think will work as is.
<?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);
?>
I'm not sure you want to send the php attachment as an octet-stream, as I do not think that is the appropriate identification for a php script. I modified it to be php's correct mime type.
For further reference, check out this link to the Mail_Mime manual.
精彩评论