0

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 smtp 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);
Community
  • 1
  • 1
AgA
  • 2,078
  • 7
  • 33
  • 62
  • You're setting two different MIME types, choose one; it cannot be the actual problem, but it's not right nonetheless. Either use a text e-mail or an html one! – Damien Pirsy Aug 02 '11 at 05:05

2 Answers2

4

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.

mzhang
  • 392
  • 3
  • 12
  • actully it can, but the point is to use it to test mail is working at all on your server –  Aug 02 '11 at 05:05
  • Sorry, I meant for you to test your server first to see if it is capable of sending emails in the first place - in this case, if the call to mail() returns false, it may be that your server isn't set up correctly. – mzhang Aug 02 '11 at 05:05
  • I can send emails but I also want to send attachment. See my thread: http://stackoverflow.com/questions/6907235/attaching-file-in-the-php-mail-code – AgA Aug 02 '11 at 05:08
  • Even the [php page about mail](http://php.net/manual/en/function.mail.php#example-2927) recommends that complex emails that require MIME are better to be sent with Mail_Mime. I posted an answer that should fix the issue. – Josh Aug 02 '11 at 05:12
  • Which settings do I need do to make : mail($to, $subj, $body) work? – AgA Aug 02 '11 at 05:44
0

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.

Josh
  • 448
  • 2
  • 9
  • how to copy paste this code? If I paste in notepad whole code gets pasted in single line. – AgA Aug 02 '11 at 05:30
  • It copies fine for me. Try pasting it into wordpad, wordpad respects line termination characters better. – Josh Aug 02 '11 at 05:31