954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP mail won't send

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 [email]my@email.com[/email], 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.

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

do you know that in order for you to use php mail() function, u need to set-up a working email system first.

http://www.w3schools.com/php/php_ref_mail.asp

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 


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

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

just read the link for better understanding...

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

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

:)

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 


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.

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

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.

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

hey zagga i also had problem on sending mail, i cannot send even if i edited my php.ini mail setting i get from thunderbird SMTP setting.

[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 = vaultdweller123@gmail
vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

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 = [email]MY_EMAIL@gmail.com[/email]


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
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 
@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?

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

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!

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

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.

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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 :)
?>
freiheit
Newbie Poster
24 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

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

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

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.... :(

madkat3
Newbie Poster
19 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

Zagga

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: