PDA

View Full Version : PHP Mail problem



Maqrkk
June 18th, 2008, 09:30 AM
I built a small mail example, to test how the PHP mail() function works.

It didn't work though, I got this error:


Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\xampp\htdocs\Mark\PHP\PHPMAIL\mail.php on line 7

I've looked it up but I can't seem to find what's wrong. I think it's running on an Apache server. How can I fix this?

actionAction
June 18th, 2008, 01:10 PM
Find you httpd.conf file and change the following lines to suit:

[mail function]
;For Win32 only.

SMTP = smtp.your_service_provider.com

smtp_port = 25

;For Win32 only

sendmail_from = webmaster@your_domain.com


Restart Apache...

Maqrkk
June 18th, 2008, 02:25 PM
Thanks :) I'll see if I can do that.. :o

Maqrkk
June 19th, 2008, 07:14 AM
I've found the file httpd.conf, but those lines are not in there. Am I doing it wrong?

Maqrkk
June 19th, 2008, 07:46 AM
I found the lines you copied there in the php.ini file. For me it says:



[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

Should I change SMTP = localhost then?
And how about the ';' before sendmail_from, remove it?
I haven't got a domainname, is that a problem..?

Maqrkk
June 19th, 2008, 10:36 AM
UPDATE

It's now working, I sent an e-mail to myself from the site. My hotmail said it was unsafe though, how come? This is the code I'm using:


<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subj'];
$message = $_POST['message'];

mail($to,$subject,$message,"From: " . $from);

?>

Should I add more in the mail function?
Note, I'm not going to let anyone fill in the form, this was a test. I'm going to use the mail function with a standard sender, no one can change that.

djheru
June 19th, 2008, 01:41 PM
You should read the comments on the PHP.net mail() page. You need to add additional mail headers to make your email look more "legitimate".

Maqrkk
June 19th, 2008, 02:01 PM
Thanks for redirecting :) I'll look into it. Lots of info there, I hope it will work out ;)

Maqrkk
June 19th, 2008, 03:44 PM
Ok I'm stuck at this point. I'm trying to make it look better. I want to send it to my e-mail, with my name infront of it, how is that done, is that possible?

This is my code:

$to = $_POST['toname'];
$to .= " <";
$to .= $_POST['toemail'];
$to .= ">";
$from = $_POST['from'];
$subject = $_POST['subj'];
$message = $_POST['message'];

echo $to;

mail($to,$subject,$message,"From: " . $from);

However, when I'm echoing $to, it only echoes the first bit, not the entire line.

Maqrkk
June 20th, 2008, 07:00 AM
I found out Windows pc's don't handle that very well. Well no troubles.
Actually.. I do have a trouble. The e-mail I send to myself is being flagged as spam/innapropriate or whatever. I'm using hotmail. I'm sending the mail from @live.nl to @hotmail.com. However, the server is hosted at xs4all.nl. Is this why it's going wrong?

Maqrkk
June 23rd, 2008, 03:37 AM
How can I send mail from a site, without having trouble with it? How does a phpBB forum handle this for example?
Through php's mail() function I can send mail as if it were from anyone. I don't think that is right, how do I associate a certain e-mail with it? It's all pretty confusing, but I want to get this right..

Maqrkk
June 23rd, 2008, 07:03 AM
I've now integrated my mail into my main site, to see if it works.

It works, it sends the e-mail. This is what my Hotmail says when I open the mail:


This message has been blocked for your safety.
And

This message may be dangerous.

I tell Hotmail to open it anyway, because I know it is safe. Users on my site however might now know that!

Also, this is what I get when I open the mail:


Hello Maqrkk,\n\nThis e-mail has been sent to you to reset your password. If you do not want to reset your password, ignore this e-mail.\n\n

I thought the \n would make a new line, but it doesn't!

Here is my code for this part of the site:


$subject = 'Email Recovery';
$message = 'Hello '.$user.',\n\nThis e-mail has been sent to you to reset your password. If you do not want to reset your password, ignore this e-mail.\n\n';
$headers = 'From: markpeerdeman@live.nl' . "\r\n" . 'Reply-To: markpeerdeman@live.nl' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($email,$subject,$message,$headers);
header("location:index.php?mode=successrec");

Can anyone help me to:
1. Get new lines to appear correctly.
2. Make this e-mail not appear as dangerous?

Thanks in advance.

actionAction
June 23rd, 2008, 12:55 PM
Try reversing your single and double quotes. By saying that I mean:


$message = "Hello ".$user.",\n\nThis e-mail has been sent to you to reset your password. If you do not want to reset your password, ignore this e-mail.\n\n";
$headers = "From: markpeerdeman@live.nl" . '\r\n' . "Reply-To: markpeerdeman@live.nl" . '\r\n' . "X-Mailer: PHP/" . phpversion();

For a reason unknown to me, line breaks (\n) only seem to work within double quotes.

Maqrkk
June 23rd, 2008, 01:04 PM
Thanks I'll try it out. Any idea what I can do to prevent Hotmail from tagging it as spam?