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"=>'[email protected]', "Subject"=>"hello" ,"Reply-To"=>"[email protected]");

$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('[email protected]', $hdrs, $body);
1
  • 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! Commented Aug 2, 2011 at 5:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

actully it can, but the point is to use it to test mail is working at all on your server
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.
I can send emails but I also want to send attachment. See my thread: stackoverflow.com/questions/6907235/…
Even the php page about mail recommends that complex emails that require MIME are better to be sent with Mail_Mime. I posted an answer that should fix the issue.
Which settings do I need do to make : mail($to, $subj, $body) work?
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'    => '[email protected]',
              'Subject' => 'Hello',
              'Reply-To' => '[email protected]'
              );

$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('[email protected]', $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.

2 Comments

how to copy paste this code? If I paste in notepad whole code gets pasted in single line.
It copies fine for me. Try pasting it into wordpad, wordpad respects line termination characters better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.