Home ยป Php ยป Sending mail with php – PHP

Sending mail with php – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

php mail… I did this ages before but I’m having problems with it.


<?php
//--- Start mail script ---
$to = "[email protected]";
$from = "[email protected]";
$subject = "Please work";
$body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
$header = "Blah blah";
$success = mail($to, $subject, $body, $header);
if(!$success) {
echo "error";
} else {
echo "success";
}
//--- End mail script ---
?>

Can anyone explain why this won’t work. I keep getting my "error" echo.

A(Answer):

The from should go into the 4th parameter (also called the header).
[PHP]<?php
//— Start mail script —
$to = "[email protected]";
$from = "[email protected]";
$subject = "Please work";
$body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
$header = "Blah blah";
$success = mail($to, $subject, $body, $from);
if(!$success) {
echo "error";
} else {
echo "success";
}
//— End mail script —
?>[/PHP]

Ronald ๐Ÿ˜Ž

A(Answer):

For some reason this still doesn’t seem to work.

I even tried putting in the parameter without using variables it just won’t work : (

A(Answer):

I’ve never seen it assigned to a variable before. Whenever I’ve sent an e-mail or seen code for it it’s always looked like:

[php] $to = [email protected]
$subject = //whatever the subject is
$text = //body text of the e-mail
$headers = //e-mail headers

mail($to, $subject, $text, $headers);
[/php]

As I said, I haven’t seen it been assigned to a variable before. I find it hard to believe that could be the problem. But I also find it hard to believe ronverdonk can be wrong.

Hope this helps
LB

A(Answer):

I am very sorry, I made a mistype in the code I showed. I forgot to prefix the sender with the literal ‘From: ‘. The following is correct, so it should be okay for you now.
[php]<?php
//— Start mail script —
$to = "[email protected]";
$from = "From: [email protected]";
$subject = "Please work";
$body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
$header = "Blah blah";
$success = mail($to, $subject, $body, $from);
if(!$success) {
echo "error";
} else {
echo "success";
}
//— End mail script —
?>
[/php] Ronald ๐Ÿ˜Ž

A(Answer):

sender’s email address should be appended in header
so you code should look like this


$to="[email protected]
$subject = "this i subjec\r\n";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To:[email protected]\r\n";
$headers .= "Return-Path: [email protected]\r\n";
$body= "type your messege here ";
mail("$to","$subject","\n$body", "$headers\r");

A(Answer):

$to="[email protected]
$subject = "this i subjec\r\n";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To:[email protected]\r\n";
$headers .= "Return-Path: [email protected]\r\n";
$body= "type your messege here ";

mail("$to","$subject","\n$body", "$headers\r");

From the mail settings in this case only the ‘From: ‘ is required in the 4th mail parameters (also called headers in the docs). The other header content is nice but NOT mandatory.

Ronald ๐Ÿ˜Ž

A(Answer):

IT WON’T WORK!!! Aaargh! why?!?
even copying and pasting, won’t work!?!


$to = "[email protected]";
$subject = "Subject";
$body = "Body text and such stuff";
$header = "From: [email protected]";
$success = mail($to, $subject, $body, $header);
if(!$success) {
echo "error";
} else {
echo "sent";
}

Now from what I gather this should work provided that "[email protected]" is replaced with a valid address. But it doesn’t! I keep getting "error". I even tried it without assigning the function to variable $success… same story. I don’t understand what sort of minute syntax tithle is missing here.

I appriciate all the help, guys though this is UNIMAGINABLY trying ๐Ÿ™

A(Answer):

Okay… I ran phpinfo() and under the section called Configuration PHP CORE I got this…

disable_functions: getmyinode, getopt, getrusage, extension_loaded, dl, mysql_pconnect, crack_check, crack_closedict, crack_getlastmessage, crack_opendict, fsockopen, pfsockopen, mysql_list_dbs, mysql_stat, ini_get, ini_get_all, ini_alter, ini_set, get_current_user, get_defined_constants, get_include_path, php_ini_scanned_files, php_uname, phpcredits, restore_include_path, set_include_path, set_time_limit, version_compare, zend_version, getmypid, getmyuid, getmygid, assert_options, assert, fopen, fwrite, fread, file, fpassthru, file, mail, opendir, readdir, closedir

This seems to be getting more complicated than i care for. Can anyone please tell me why are some functions disabled and why is mail one of those functions and how can i enable some of these function or can i not.

ronverdonk?

A(Answer):

You most probably are running PHP in safe mode. That mode is set at installation of PHP. This mode disables the functions you mention, see also the list of disabled functions at Functions restricted/disabled by safe mode

The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren’t very realistic, many people, especially ISP’s, use safe mode for now.

See the PHP manual for a discussion on safe mode at Safe Mode

If you installed PHP yourself, remove the safe mode. If not, you’d better have a talk with your server provider.

Ronald ๐Ÿ˜Ž

A(Answer):

Okay, thanks. I’ve emailed my server provider… they’ve promised a quick response so I’ll see.

At least I can now stop bashing my head against the wall.