Jump to content

Archived

This topic is now archived and is closed to further replies.

yitzle

Basic mail() usage

Recommended Posts

Help?!
My code:
[code]<?php
mail('[me]@gmail.com', 'My Subject', 'Testing');
?>[/code]
That's thye entire .php
I direct my browser to the php file and check my email. Hmm. Nothing.
[code]<?php
@$test=mail('[me]@gmail.com', 'My Subject', 'Testing');
if ($test==1){
echo('True');
} else {echo('False');}
?>[/code]
Well. I get 'True' but still no email.
I'm using 100webspace.net
What's going on? Why won't it work?

Thanks!

Share this post


Link to post
Share on other sites
You may want to check and see what your host requires for out going mail . I would do something like this

[code]
<?php
$to = "someone@something.com";
$subject = "A test message";
$message = "This is a test message. I am trying to
see if this mail function will work for me";

if (mail($to, $subject, $message, "From:You<you@yoursite.com>\nX-Mailer: PHP/" . phpversion())) {
     echo "You mail was sent";
}else{
     echo "Your mail was not sent";
}
?>[/code]


Some host's mail servers require the From: section to an address that is known to the mail server.

Good luck,
Tom

Share this post


Link to post
Share on other sites
Not working? Did you get an error?  Like I said you may want to contact your host. Are you makeing sure that the from portion of the code is a valid email address with the mail server?

Share this post


Link to post
Share on other sites
Erm... The mail() returns true
Can you elaborate on "Are you makeing sure that the from portion of the code is a valid email address with the mail server?"
Does it need to be valid format or a working address?
Does the @abc.com have to match the host's domain name?

Share this post


Link to post
Share on other sites
some mail servers require that the FROM you@yourdomain.com be a valid email address. This must be a address that is known to the server. For instance, webmaster@yourdomain.com or whatever.

Share this post


Link to post
Share on other sites
Sorry for being obtuse, but I don't follow.
My PHP is on 100webspace.net
The email is going to gMail.com
Who is imposing the From?
Is it gMail? And is gMail requiring it for be a recognized email address (e.g. a gMail address) or a valid @100webspace.net (which I don't have ATM)?
Or is it 100webspace?

Share this post


Link to post
Share on other sites
do you have a domain or is it a subdomain? If it is one of those free sites then they may not allow you to send mail at all. I would contact them and ask them if you are allowed to send mail via php and if so what requirements do you have to meet.

Share this post


Link to post
Share on other sites
Here is a working snippet from a site I am working on for work..

[code]
<?php

$msg = "Name: ".$_POST['name']."\nAddress: ".$_POST['address']."";
if ($_POST['address2'] != "") {
$msg .= "\nAddress 2: ".$_POST['address2']."";
}
$msg .="\nCity: ".$_POST['city']."\nState: ".$_POST['states']."\nZip Code: ".$_POST['zip']."";
$msg .= "\nEmail: ".$_POST['email']."\nPhone Manufacturer: ".$_POST['makes']."\nPhone Model: ".$_POST['modelnum']."\nBlue Light?: ".$_POST['blue_light']."\nComments: ".$_POST['comments']."";
$from = "FROM: ".$_POST['email'];
mail('to@domain.com', 'faq form submission', $msg, $from);

?>
[/code]

Okay so all of the $msg is taking contents of a form and putting them together into an email message.. 
$from is the person who is sending the email address..  it needs to be there otherwise it won't work right...  even if you just put in there 'FROM: myemail@mydomain.com' it will work...

It's really not that hard...  basically odds are you are going to have a form.. in this form you should ask for the persons email address -- when they input their email address this now becomes your from address..

Share this post


Link to post
Share on other sites
I am in fact using a form. And it does have a mandatory email field :)
However, I'm pulling my data by doing [code]$msg .= "From: " .$from;[/code] where from is the name of the textfield on the form.

I'll send the admins an email about sending mail().
It is a subdomain setup, but I think I am allowed an email address at it.

Share this post


Link to post
Share on other sites
From the 100webspace.com PHP FAQ:
[quote]You can be having trouble sending e-mail using Formmail script, because of the following reasons:

1. You have entered an e-mail address which is not hosted on our servers. We require the one of either the 'FROM:' e-mail address or the 'TO:' e-mail address to be hosted on our servers. Only if one of them is hosted on our servers, you will be able to send e-mail successfully.

2. You are using wrong header information. You must always provide the text From:, the name of the sender and an e-mail address. Without one of these three parameters, the formmail script will not work properly and will not deliver e-mail to your mailbox. You can find out more information here: http://www.php.net/manual/en/function.mail.php

Here are examples of well working formmail scripts:

First Example:

<?
$from = "From: yourname ";
$to = "receiver";
$subject = "Hi! ";
$body = "TEST";

if(mail($to,$subject,$body,$from)) echo "MAIL - OK";
else echo "MAIL FAILED";
?>

Second Example:

<?
$from = "From: sender";
$to = "yourname ";
$subject = "Hi! ";
$body = "TEST";

if(mail($to,$subject,$body,$from)) echo "MAIL - OK";
else echo "MAIL FAILED";
?>[/quote]
[move]Oops :)[/move]
Thanks for all the time and effort from you guys!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.