Send email using PHP

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
It is, in fact, possible. There's even a built-in function that does exactly that:
PHP:
mail("to","subject","message","headers");
"to" is who you're sending the email to.

"subject" is the subject line of the email.

"message" is the email message itself.

"headers" are things like From:, Cc:, and Bcc: in that order. You must at least include a From: address, the other two fields are optional. Each field in here should be separated by a newline character ("\n").

Example:
PHP:
mail("to@example.com", "Test Email Message", "This is a test email message, please disregard.","from@example.com");
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
The mail() function is indeed the basic way to send mail using php.
However, you made a mistake with the headers. All headers must be separated by "\r\n", not just by "\n". And this is wrong in your example too...
PHP:
$to = "to@example.com"; //Other possibilities: "Name <to@example.com>"
$subject = "Test Email Message"; //Plain text
$message = "This is a test email message, please disregard."; //You can also use HTML or any other content type here.
$headers = "From: $from"; //The "From: " part is required!
$headers .= "\r\n"; //Separation mark
$headers .= "Content-type: text/plain"; //You aren't limited to just from, bcc etc headers.
$headers .= "\r\n\r\n"; //Double one to end it.
mail($from, $subject, $message, $headers);

mail headers are like html headers, you can give loads more than just from.

- Marshian
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
The mail() function is indeed the basic way to send mail using php.
However, you made a mistake with the headers. All headers must be separated by "\r\n", not just by "\n". And this is wrong in your example too...
PHP:
$to = "to@example.com"; //Other possibilities: "Name <to@example.com>"
$subject = "Test Email Message"; //Plain text
$message = "This is a test email message, please disregard."; //You can also use HTML or any other content type here.
$headers = "From: $from"; //The "From: " part is required!
$headers .= "\r\n"; //Separation mark
$headers .= "Content-type: text/plain"; //You aren't limited to just from, bcc etc headers.
$headers .= "\r\n\r\n"; //Double one to end it.
mail($from, $subject, $message, $headers);
mail headers are like html headers, you can give loads more than just from.

- Marshian

You don't need a double or even a single CRLF(\r\n) at the end of the headers. The last header doesn't need to be followed by anything(with mail(), not speaking generally about http headers).

Also, I know it's just an example but it's worth mentioning that $from would have to be sanitized to exclude injections of additional headers. You'd usually use a regular expression or other such restrictive validation for the rare cases you use user input in headers. But at the least, you shouldn't allow \r or \n.
 

gcottick

New Member
Messages
11
Reaction score
0
Points
0
Some sites seems pretty limited on the mail functions but everything I am trying on x10 (with intermediate PHP) seems to be working out fine.

Does anyone know the limit on the number of addresses in a BCC (no, this is not for spamming purposes!!?

Thank... Cy
 

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
The mail() function is indeed the basic way to send mail using php.
However, you made a mistake with the headers. All headers must be separated by "\r\n", not just by "\n".
My bad. Too used to thinking of \r\n as a Windows Server thing.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
If this topic is still open.
Here is a function I use to construct html mail headers.

PHP:
   function constructHeader() // Construct the mail headers
	{
		$this->Header  = 'MIME-Version: 1.0'."\r\n";
		$this->Header .= 'Content-Type: text/html;charset=iso-8859-1'."\r\n";
		$this->Header .= 'X-Priority: 3 (Normal)'."\r\n";
		$this->Header .= 'X-MSMail-Priority: Normal'."\r\n";
	
		$this->Header .= 'To: '.$this->ToName.' <'.$this->ToAdd.'>'."\r\n";
		$this->Header .= 'Return-Path: '.$this->ToAdd."\r\n";
		$this->Header .= 'From: '.$this->FromName.' <'.$this->FromAdd.'>'."\r\n";
		$this->Header .= 'Reply-To: '.$this->FromName.' <'.$this->FromAdd.'>'."\r\n";
		$this->Header .= 'X-Mailer: PHP/'.phpversion();
	}

its OO but can be easily changed to straight php :p
 
Last edited:
Top