Jump to content

Sending Mail using PHP


Meriakh
 Share

Recommended Posts

So I've coded this short php snippet to test if I can send emails from my server. I set up the smtp server and opened ports on my firewall/router. I also set up my php.ini file with what I think are the correct inputs. I then restarted both the smtp and apache servers. When I run this code it outputs "Mail Sent!", but I am getting nothing sent to my email address. Is there something I am missing here?

<?php$to = "user1@gmail.com";$subject = "HTML email";$message = "Hi";// Always set content-type when sending HTML email$headers = "MIME-Version: 1.0" . "\r\n";$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// More headers$headers .= 'From: user2@gmail.com' . "\r\n";if(mail($to,$subject,$message ,$headers)){    echo "Mail Sent!";}else{    echo "Mail Failed to be sent";}?>

Link to comment
Share on other sites

Guest So Called

Reduce it to the minimum:

mail("user1@gmail.com","subject","message,"")

I think you'll find your problem is downstream, just a hunch. IOW your local server configuration or related. If you have a local host SMTP server then try configuring your email client to connect through that. You said you restarted your SMTP server... I don't know why you even need one, since you probably have to connect that to your ISP. You could probably connect to your ISP directly from Apache/PHP.

Link to comment
Share on other sites

Decided to try to use pear to send my emails and have come up with this code. Installed pear and pear mail.

<?php include "C:/php/pear/Mail.php"; require_once "Mail.php";  $from = "user1@gmail.com>"; $to = "user2@yahoo.com>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?";  $host = "ssl://smtp.gmail.com"; $port = "465"; $username = "user1@gmail.com"; $password = "pass";  $headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject); $smtp = Mail::factory('smtp',   array ('host' => $host,	 'port' => $port,	 '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>");  } ?>

and I am getting caught at this line in particular:

$mail = $smtp->send($to, $headers, $body);

Guess I'm wondering what could be causing this line not to run - I am at a bit of a loss here. Opened port 465 in my firewall/router, restarted apache. I've seen some very similar code online, so not sure what makes what I'm doing different. I am on a Windows XP box btw.

Link to comment
Share on other sites

So I turned on the error log for php. Managed to get some interesting output but haven't decided what to make of it yet. [23-Jun-2012 22:38:27] PHP Warning: include_once(Net/SMTP.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in C:\php\PEAR\Mail\smtp.php on line 348[23-Jun-2012 22:38:27] PHP Warning: include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Net/SMTP.php' for inclusion (include_path='.;C:\php\pear\') in C:\php\PEAR\Mail\smtp.php on line 348[23-Jun-2012 22:38:27] PHP Fatal error: Class 'Net_SMTP' not found in C:\php\PEAR\Mail\smtp.php on line 349[23-Jun-2012 22:38:28] PHP Warning: include_once(Net/SMTP.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in C:\php\PEAR\Mail\smtp.php on line 348[23-Jun-2012 22:38:28] PHP Warning: include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Net/SMTP.php' for inclusion (include_path='.;C:\php\pear\') in C:\php\PEAR\Mail\smtp.php on line 348[23-Jun-2012 22:38:28] PHP Fatal error: Class 'Net_SMTP' not found in C:\php\PEAR\Mail\smtp.php on line 349[23-Jun-2012 22:38:36] PHP Notice: Undefined variable: mail in C:\website\mailpractice.php on line 32

Link to comment
Share on other sites

it means that file you are trying to include is not there. you have to make sure it is there

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...