Bear with me, I'm new here and new to PHP...at first I thought it was just me, so I started trying to copy and paste example code I found online...but even that seemed not to work.

I've tried this:

$to = "my@email.com";
$subject = "BUSINESS";
$message = "blah";
$headers = "From: my@email.com"


$send_contact = mail($to,$subject,$message,$headers);

// Check, if  message sent to your email "We've recived your information"
if($send_contact){ echo "We've recived your contact information"; }
else { echo "ERROR"; }

And even just...

mail("my@email.com","BUSINESS","blah","From:  my@email.com");

And finally this:

$to = "my@email.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: my@email.com" . "\r\n" .
"CC: somebodyelse@example.com";

if(mail($to,$subject,$txt,$headers)){ echo "We've recived your contact information"; }
else { echo "ERROR2"; }

I've tried it with double "quotes", single 'quotes', putting the information I wanted directly in, skipping the variables altogther, removing the function except for the last if statement to see if it gives an error. Now obviously I used my own personal emails here, and not my@email.com, but you get the idea...

The server is a windows server (unfortunately :p) if that makes any difference.

Please help me out...What am I doing wrong here? It always seems to at least show "Error" so I'm pretty sure it isn't invalid syntax since when that's the issue I usually just get a white blank page with Firefox.

Recommended Answers

All 29 Replies

Do you mean like Thunderbird? Or is there a specific email system for PHP? Because if not, I already have a regular email client.

just read the link for better understanding...

Vaultdweller123 is correct I think. Read the link, it should really help.
Your code is fine.

:)

Umm...I read the link before I posted my response...I still didn't get it because if it was already installed in the PHP core, why would it need something else?

Requirements

For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
Installation

The mail functions are part of the PHP core. There is no installation needed to use these functions.

Member Avatar for Zagga

Hi madkat3,

You are almost there.
Php uses the sendmail function to, well, send mail, and this is installed in the core PHP so you don't need to worry about that.
To actually send the mail though, it needs to use a SMTP server, just like Thunderbird does, so you need to tell PHP the SMTP details, just like you had to tell Thunderbird.

To do this in PHP you need to edit the php.ini file. Open your PHP.ini file and find the lines that look something like this ...

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

(I suggest you search for 'smtp' as php.ini can be a big file)

You need to change 'localhost' to whatever your SMTP server is and you need to change the smtp_port to whatever SMTP port you use. You can get these details from Thunderbird.
Stop your server, then start it again so it uses the new php.ini settings and try to send the mail again.

Hope this helps.
Zagga

Hi madkat3,

You are almost there.
Php uses the sendmail function to, well, send mail, and this is installed in the core PHP so you don't need to worry about that.
To actually send the mail though, it needs to use a SMTP server, just like Thunderbird does, so you need to tell PHP the SMTP details, just like you had to tell Thunderbird.

To do this in PHP you need to edit the php.ini file. Open your PHP.ini file and find the lines that look something like this ...

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

(I suggest you search for 'smtp' as php.ini can be a big file)

You need to change 'localhost' to whatever your SMTP server is and you need to change the smtp_port to whatever SMTP port you use. You can get these details from Thunderbird.
Stop your server, then start it again so it uses the new php.ini settings and try to send the mail again.

Hope this helps.
Zagga

Thank you Zagga...After searching for a bit after my last post, I was able to find what you just mentioned. My only thing is, Thunderbird was only set up pop 3, I don't know anything about the SMTP server, and it seems I'll have to set one up myself.

The unfortunate part is, out of all the Linux computers that are solely mine, the shared PC is the WinXP computer acting as the Apache server. Since it's the best computer, my roommates need something they can "understand" lol. Otherwise I'd have fedora on it too. Is there any good free/open source SMTP server for XP?

Or is there a more painless solution here? I read SMTP servers eat up a lot of the CPU...All I want to do is be notified when someone submits a request on my website. Literally it makes a copy and stores that in a text file so I know what they want to buy, but I wanted it to email me a notification when somethings been added to that text file. Thanks.

Member Avatar for Zagga

Hi again madkat3,

Pop3 (or IMAP) deals with receiving emails. SMTP deals with sending emails. You don't need to set up your own SMTP server as you can use one that already exists. If you can send emails through Thunderbird (or any other desktop email client) then you can use the same SMTP server that it uses.

In Thunderbird:
"Tools" -> "Account Settings" -> On the page that opens, on the left side, at the very bottom there should be an "Outgoing Server (SMTP) link. If you click this, it will show you what SMTP server Thunderbird is using. You need to note down the Server Name and the Port.

If you enter these details into Php.ini then you should, fingers crossed, be able to send email.


Zagga

Okay yeah, so I originally tried one email, and since the above post now I wanted to see if it would work with my gmail with IMAP, and I got the settings right out of Thunderbird, and I even tested it to make sure it would send email, and it still sends fine from thunderbird to that email, but when I put it in the form, I still get an error.


Here's what my php.ini file says:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.googlemail.com
; http://php.net/smtp-port
smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = MY_EMAIL@gmail.com


Are there login details it needs? I have them right in Thunderbird and left it open just in case that would affect it.


EDIT: I restarted my sever a few times, and now it seems to not be doing anything...no error, loads the other pages fine, but when hitting submit it just sits there will the loading circle spinning in Firefox. The CPU is at 4% so it's not the load...it's just not doing anything... :(

@madkat3 yeah dude same here, when i hit send, it's just loads a never ending loading

@madkat3 yeah dude same here, when i hit send, it's just loads a never ending loading

All I can say is, what the hell?

...Is it a gmail thing? Or just a "mail();" thing?

i dont know... but one thing is for sure, we had set correctly the php.ini coz at first i was seeing errors but now no error only a never ending loading upon submit

http://articles.sitepoint.com/article/advanced-email-php/1

The above link will show how to configure for mail, sending mail from php, mail attachments, with/without HTML formats and all about the php mail.

Hope this helps!

...That's the article I was referring to when I said this:

Thank you Zagga...After searching for a bit after my last post, I was able to find what you just mentioned.

...it says to restart the server and you're ready to go...but you aren't... :( At least I'm not the only one with the never ending loading upon submit.

Try this

<?php
  $Name = "Da Duder"; //senders name
  $email = "email@adress.com"; //senders e-mail adress
  $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
  $mail_body = "The text for the mail..."; //mail body
  $subject = "Subject for reviever"; //subject
  $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
  mail($recipient, $subject, $mail_body, $header); //mail command :)
?>
Member Avatar for Zagga

Hi again folks,

I have my mail function working correctly, but when I changed the details to the gmail smtp server, it just hangs, the same as is happening with you.
I then tried Yahoo details and got an error saying that it needs to be authenticated.
So, gmail needs to know your username and password, but we have no way of supplying this info :(

I got it working using the SMTP details of my webhosting account (which comes with email accounts) so I assume this SMTP does NOT require authentication.

All I can think of is if you have a webhost, try using that or maybe try using your ISP's SMTP details.

Zagga

Try this

<?php
  $Name = "Da Duder"; //senders name
  $email = "email@adress.com"; //senders e-mail adress
  $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
  $mail_body = "The text for the mail..."; //mail body
  $subject = "Subject for reviever"; //subject
  $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
  mail($recipient, $subject, $mail_body, $header); //mail command :)
?>

...Yeah I tried that up and down and backwards lol.

Hi again folks,

I have my mail function working correctly, but when I changed the details to the gmail smtp server, it just hangs, the same as is happening with you.
I then tried Yahoo details and got an error saying that it needs to be authenticated.
So, gmail needs to know your username and password, but we have no way of supplying this info :(

I got it working using the SMTP details of my webhosting account (which comes with email accounts) so I assume this SMTP does NOT require authentication.

All I can think of is if you have a webhost, try using that or maybe try using your ISP's SMTP details.

Zagga

Yeah, see I thought it was something to do with the login details in my other post. Unfortunately I don't have an SMTP server I can use that doesn't have a username and password.... :(

Member Avatar for Zagga

Give them a try anyway (just the SMTP and port, not your username and password), you may get lucky :)

Zagga

Member Avatar for Zagga

I just had a look into that and it seems to be the standard class for mailing.
It is the first time I have used PEAR but is was very easy to set up.

The article you linked has some great code you can just copy and was easier to follow than the documentation for the class.

Hope you get it working.


Zagga
p.s. I managed to get the "SMTP Authentication and SSL Encryption" example to send email via my gmail account.

Okay, I tried...it still won't work.

I did this:

http://www.geeksengine.com/article/install-pear-on-windows.html

Then downloaded this package:

http://pear.php.net/package/Mail/download

...And copied the Mail.php and Mail folder over to where the PEAR includes are. Then came back to this page:

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

...and used the exact snippet they offer with:

<?php
 require_once "Mail.php";
 
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 
 $host = "mail.example.com";
 $username = "smtp_username";
 $password = "smtp_password";
 
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     '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 changed it to this:

<?php
 require_once "Mail.php";
 
 $from = "Test <sender@example.com>";
 $to = "Myself <MYREALGMAILACCOUNT@gmail.com>";
 $subject = "Test";
 $body = "Hi,\n\nHow are you?";
 
 $host = "smtp.googlemail.com";
 $username = "MYREALGMAILACCOUNT@gmail.com";
 $password = "MYPASSWORD";
 
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 
 $mail = $smtp->send($to, $headers, $body);
 
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>We've received your inquiry. Thank you!</p>");
  }
 ?>

I finally had the chance to work on it again...but it still won't work though...I just don't get it. :( It just comes up with a blank page and no confirmation. I tried it with the SSL way it shows as well, and I changed the smtp server to ssl://smtp.googlemail.com and also just smtp.googlemail.com with $port = "465"; ...what am I doing wrong? It's not even giving an error, just a blank page, when the PHP script says it should provide some feedback, and that aside I'm not even passing the variables anymore, just trying to directly provide the values without sending them from the other page altogether.

Member Avatar for Zagga

Hi again madkat3,

The code I used was slightly different to what you posted.
Try using this (changing the $from $to $username and $password variables) and see if it works.

<?php
require_once "Mail.php";

$from = "Your name <your_email_address@gmail.com>";  // This should be your Gmail address.
$to = "To Name <to_email_address@whatever.com>";  // This is who you want to sent the message to.
$subject = "From Google";
$body = "Hi,\n\nHow are you?";

$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "YourRealGmailAccount@gmail.com";
$password = "YourGmailPassword"; 


$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>");
  }
?>

This code has the a slightly different $host and also includes the port to use, where it is missing from the code you used.

Zagga

Hi again madkat3,

The code I used was slightly different to what you posted.
Try using this (changing the $from $to $username and $password variables) and see if it works.

<?php
require_once "Mail.php";

$from = "Your name <your_email_address@gmail.com>";  // This should be your Gmail address.
$to = "To Name <to_email_address@whatever.com>";  // This is who you want to sent the message to.
$subject = "From Google";
$body = "Hi,\n\nHow are you?";

$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "YourRealGmailAccount@gmail.com";
$password = "YourGmailPassword"; 


$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>");
  }
?>

This code has the a slightly different $host and also includes the port to use, where it is missing from the code you used.

Zagga

Thanks for your help again. I copied and pasted your code exactly as is. It's that I want it to actually send to the same gmail account it's comming from. It doesn't need to go to anyone but me, since it's more or less an alert. In my other post I'd mentioned I'd tried it with and without the SSL and Port. It's strange to me because not only does it not go through, but it doesn't even give an error message, and that's the only code in the php page that the other page submits to. And the variables it's carrying over aren't even being used, so I believe that issue is eliminated as well. Still same thing, just a blank and empty page... :(

Hey, madkat3

Try this, Some of windows servers didn't allow to send emails from a different account rather than their own:

Change

$headers = "From: my@email.com"; 
//into something like this:
$headers = "From: system@yourowndomain.com"

Hopefully it will work, you should also know that these emails will always arrive in Spam/Junk folder, you will need to add more headers to receive in Inbox.

Member Avatar for Zagga

Hi again madkat3,

This issue has intrigued me so I downloaded the Mail class that you linked to Then downloaded this package:

http://pear.php.net/package/Mail/download and I noticed it didn't include a few files that are in my Mail package. The Mail class says that Net_SMTP is optional but if I remove it, I get a page full of errors. Net_SMTP requires another package, Net_Socket.
Try replacing your Mail folder (in your PEAR folder) with the one I have attached. Then try the code I previously mentioned<?php
require_once "Mail.php";

$from = "Your name "; // This should be your Gmail address.
$to = "To Name "; // This is who you want to sent the message to.
$subject = "From Google";
$body = "Hi,\n\nHow are you?";

$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "YourRealGmailAccount@gmail.com";
$password = "YourGmailPassword";


$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("" . $mail->getMessage() . "


");
} else {
echo("Message successfully sent!


");
}
?>

Your Mail.php may be different too, so I have also attached the version I have.

If you use the above code, and the attached files, you will have the exact same set-up as me, so it should work.
Mail Folder.zip

Mail.php


Hope this helps
Zagga

commented: Extremely helpful! Worked on the issue with me from start to finish. +1

Hi again madkat3,

This issue has intrigued me so I downloaded the Mail class that you linked to and I noticed it didn't include a few files that are in my Mail package. The Mail class says that Net_SMTP is optional but if I remove it, I get a page full of errors. Net_SMTP requires another package, Net_Socket.
Try replacing your Mail folder (in your PEAR folder) with the one I have attached. Then try the code I previously mentioned

Your Mail.php may be different too, so I have also attached the version I have.

If you use the above code, and the attached files, you will have the exact same set-up as me, so it should work.

[ATTACH]15881[/ATTACH]
[ATTACH]15879[/ATTACH]

Hope this helps
Zagga[ATTACH]15880[/ATTACH]

I'm sorry for the slow response. I'd all but given up on this form, and was even looking into alternate means, but I decided to just check in here one last time. And Zagga, I really don't know what to say, but OMG it worked!!! Thank you sooo much! Seriously, this finally ended the most frustrating error I have had this summer! I can't thank you enough. It seems, originally it was just an authentication issue, but it ended up being my mail package that was messed up or out of date. My code was correct all along. Seriously, thank you, thank you, and thank you again!

Member Avatar for Zagga

You are welcome :)

I'm glad we finally got it sorted out for you.


Zagga

Hi Madkat3, I think zagga is right try check your setting, or do it online, with hosting n domain, not on localhost:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.