mail() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

mail() function

Post by matt1234 »

When I run a page with the mail() function, I get "Recipient names must be specified"

This is just a test page, btw...

Code: Select all

echo "K.";
$msg = "Message body";
$msg. = "Continued \n";
$recip = "<webmaster@mywebsite.com>";
$subject = "Test E-mail";
$mailheader = "From: Our E-mail Account <email@mywebsite.com> \n";
$mailheader .= "Reply-To: <email@mywebsite.com> \n";
 
$mail($recip,$subject,$msg,$mailheader);
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mail() function

Post by Christopher »

I think $recip should be either "Webmaster <webmaster@mywebsite.com>" or just "webmaster@mywebsite.com"
(#10850)
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

No such luck, unfortunately
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: mail() function

Post by becky-atlanta »

Code: Select all

# $mail($recip,$subject,$msg,$mailheader);
I think $mail does not need the $. I have seen example with @, i.e. @mail(...).
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

Sorry, you're right about that. I retyped it on the forum but my actual code doesn't have the "$"
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: mail() function

Post by becky-atlanta »

These are examples from http://us3.php.net/function.mail:

----------------------------------------------------------------------------
to

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

* user@example.com
* user@example.com, anotheruser@example.com
* User <user@example.com>
* User <user@example.com>, Another User <anotheruser@example.com>
-------------------------------------------------------------------------------

I used them before and they worked.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: mail() function

Post by Apollo »

I think the problem is here:
matt1234 wrote:$recip = "<webmaster@mywebsite.com>";
Use either "webmaster@mywebsite.com" or "Matt <webmaster@mywebsite.com>". See RFC specs above.
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

Code: Select all

<?php
// The message
session_start();
$message = "Line 1\nLine 2\nLine 3";
 
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
echo "UM....";
// Send
if (mail('Webmaster <webmaster@mysite.com>', 'My Subject', $message)) {
 
echo 'E-mail sent.';
}
else {
echo "uh oh!";
}
 
?>
And I get:

UM....Recipient names must be specified E-mail sent.

I put the echo 'UM...' there just because if I don't echo something while I'm testing it and having this problem, it gives me an internal 500 error.

I took this code straight from the PHP site other than the echos, the "if", and the session_start(); I'm at a loss here.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: mail() function

Post by Apollo »

On what kind of server are you running this? Does it have an SMTP server installed? Does phpinfo() reveal anything?
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

I'm on a LAMP server
There's a SMTP
What would you like to know what the phpinfo says that would help me or help you in your helping me?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: mail() function

Post by Apollo »

No I was hoping to get some info about the SMTP. But if you say it's installed, it will probably be OK.

Does it make a difference if you try and use a dedicated mail class, like phpmailer ?
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

Mailer Error: The following From address failed: email@mywebsite.com
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: mail() function

Post by Chris Corbyn »

Is the address hand-coded, or pulled from a database? Check to make sure there's no whitespace either side of it (trim() it).
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: mail() function

Post by McInfo »

This is probably unrelated to the problem at hand, but according to RFC 2822:
Messages are divided into lines of characters. A line is a series of
characters that is delimited with the two characters carriage-return
and line-feed; that is, the carriage return (CR) character (ASCII
value 13) followed immediately by the line feed (LF) character (ASCII
value 10).
This applies to the lines in your custom header ($mailheader). Your header lines should end with:

Code: Select all

\r\n
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 11:20 am, edited 1 time in total.
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: mail() function

Post by matt1234 »

Address is hand-coded
As far as the mail header goes, thanks for the tip - I'll remember that, but the error mentions the recipient names. I don't know why it says name"s".
Post Reply