Emailing in PHP

I just made an easy emailing code, or at least, I thought it’s an easy one.
While it is working on one of my hosting, it doesn’t work on another.
It may be due to PHP4 and PHP5.

Here are the snip code:

<?php
$FirstName=$_POST[‘FirstName’];
$LastName=$_POST[‘LastName’];
$Email=$_POST[‘Email’];
$Phone=$_POST[‘Phone’];

$sujet2=“$FirstName $LastName asked for it!”;

$message2="Hi Cody ,

";

$message2.="My first name is $FirstName.
";
$message2.="My last name is $LastName.
";
$message2.="My email is $Email
";
$message2.="My phone is $Phone
";
$headers2="From: $FirstName $LastName<$Email>
";

mail(“cody@cody.com”,$sujet2,$message2,$headers2);

?>

If it is because of the PHP4/PHP5 issue (as I suspect), what can I do to make it work for both?
If not, what’s wrong?

I not sure this error is due to php4,php5. But the thing is your mail is consider as a spam one because you haven’t set the MIME Version, char type. Because some of the hosting providers checks the outgoing mails whether it is spam or not.

In quick look, it seems that the code is fine. Maybe the hosting server has disabled the main function. Try simply sending one line emails without header:


mail("cody@cody.com",'subject','message');

BTW, did you have any error message while running the above code or it just does not send the email?

Best solution for sending mail in PHP: PHPMailer

No error. Just not sending anything…
Again it works fine on another hosting…
I think it has to be with the hosting but I have no knowledge about hostings…

You don’t have to have any knowledge about hostings if you are sure the problem is because of hosting then directly ask (by emailing) them (who company/person have provided you hosting for your site) whether that function has disabled or any problems or maybe you need to send the emails only with smtp or something else.

BTW, did you test the above test line that i had suggested before in my above post?


mail("cody@cody.com",'subject','message');

It did not work either?

No, it didn’t work. No error and no sending either…

check the register_globals instead.

if(ini_get('register_globals')==0)
{
$FirstName=isset($_POST["FirstName"]) ? $_POST["FirstName"] : "";
$LastName=isset($_POST["LastName"]) ? $_POST["LastName"] : "";
$Email=isset($_POST["Email"]) ? $_POST["Email"] : "";
$Phone=isset($_POST["Phone"]) ? $_POST["Phone"] : "";
}

Because in php4 the $_POST superglobal doesn’t exists, so in the first lines you overwrite the already existing values with empty data.

have you tried

<?php
echo $message2; 
mail(--------);
?>

??

I checked the register_globals already. I tried and it didn’t work.
I tried to copy/paste your code, not working either.

have you tried

<?php
echo $message2; 
mail(--------);
?>

??

I haven’t. How should I do? echoing all variables? mail()ing them one by one? This is not clear to me…

try:

echo $message2; 

read your host’s FAQ forum about emailer issues.

OK, finally found the solution. It seems that this problem is all over the internet without any solution. I know Sitepoint coders are not very qualified and few knows their codes.
I changed my codes with these:


<?php
require_once "Mail.php";

$to = "$FirstName $LastName <$Email>";
$from = " Cody <cody@cody.com>";
$subject = " Hello $FirstName $LastName";
$body = "

Hi Cody ,\
\


My first name is $FirstName.\

My last name is $LastName.\

My email is $Email\

My phone is $Phone\


";

$host = localhost
$username = your_cpanel_username
$password = your_cpanel_password

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

I also had to install the PEAR Mail package

Care to share? I mean you switched to pear as the solution? What was the problem?

Hi

I wonder if anybody can help.

I have a Form php sending an notification email, but I want the sent from to read ‘Form Feedback’ instead of the postmaster email.

How can I add this to my php?

<?php
function htmlhead()
{

    ?>
<html>
<head>
<title>Registration Sent</title>
</head>

<body>
<?php
}

function foot()
{
?>
<form method="LINK" action="ts_contact2.html"><input type="submit" value="Return to contact page"></form>
</body>
</html><?php
}




if ( !empty( $_POST ) ) {

//TS_Email"

if(empty($_POST["TS_Email"]))
$errors[]="TS_Email is empty";


if(empty($_POST["TS_Name"]))
$errors[]="The name is required!";

if(empty($_POST["TS_Tel"]))
$errors[]="Tel is empty.";

if(empty($_POST["TS_Enquiry"]))
$errors[]="Enquiry is empty.";


    $to = "<info@macaruba.com>";
    $subject = "TS contact form";

    function stripandfilterhtml( $string )
    {
        if ( !is_array( $string ) )
            $string = htmlspecialchars( strip_tags( $string ) ) ;
        return $string;
    }


    $email = stripandfilterhtml($_POST['EAE_Email']) ;


/* Build $message... */
$message='';
$message.= "TS_Name_:  " . stripandfilterhtml($_POST['TS_Name'])  ."<br />"; // Textfield
$message.= "TS_Email:  " . stripandfilterhtml($_POST['TS_Email'])  ."<br />"; // Textfield
$message.= "Tel_____:  " . stripandfilterhtml($_POST['TS_Tel'])  ."<br />"; // Textfield
$message.= "Enquiry_:  " . stripandfilterhtml($_POST['TS_Enquiry'])  ."<br />"; // Textfield

    $headers = "From: $email";
    $headers = 'MIME-Version: 1.0' . "\\r\
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
";
    if(empty($errors))
    {
        $sent = mail( $to, $subject, $message, $headers ) ;
        if ( $sent ) {
            header( "Location: http://www.eastanglianentertainments.co.uk/pages/ts_thanks.html" );
            exit();
        } else {
            header( "Location: " . $_SERVER["PHP_SELF"] . "?email_failed" );
            exit();
        }
    }
}

htmlhead();

if ( isset( $_GET["thankyou"] ) ) {

    ?>Registration Received<?php
}

if ( isset( $_GET["email_failed"] ) ) {

    ?>There has been an issue with your registration, please try again. If the problem persists, please inform us through the contact webmaster link on the home page.<?php
}

if(!empty($errors))
print "Please check these errors:<br />". implode("<br />" , $errors);

foot();

?>

Cheers
Macaruba

[ot]

:scratch:[/ot]

Then why do you think that you come here in SPF and post the problems?

The way to do that is to place the email address in angled brackets, with the text part of the name coming before it.

As such, it would look like this:

Form Feedback <postmaster@eastanglianentertainments.co.uk>

Hi pmw

yes that looks logical but where would I have to put it within my php code, above?

The reverse is true but if you feel that way then why waste your time here. Making comments like that means that those who do know the answer to your questions will not give it to you because you have insulted them.

This is the line you would want to modify


$headers = "From: $email";

to this:


$headers = "From: Form Feedback<$email>";

Hi pmw57

I tried that and it didn’t work

this is my altered php:

<?php
function htmlhead()
{

    ?>
<html>
<head>
<title>Registration Sent</title>
</head>

<body>
<?php
}

function foot()
{
?>
<form method="LINK" action="ts_contact2.html"><input type="submit" value="Return to contact page"></form>
</body>
</html><?php
}




if ( !empty( $_POST ) ) {

//TS_Email"

if(empty($_POST["TS_Email"]))
$errors[]="TS_Email is empty";


if(empty($_POST["TS_Name"]))
$errors[]="The name is required!";

if(empty($_POST["TS_Tel"]))
$errors[]="Tel is empty.";

if(empty($_POST["TS_Enquiry"]))
$errors[]="Enquiry is empty.";


    $to = "<info@macaruba.com>";
    $subject = "TS contact form";

    function stripandfilterhtml( $string )
    {
        if ( !is_array( $string ) )
            $string = htmlspecialchars( strip_tags( $string ) ) ;
        return $string;
    }


    $email = stripandfilterhtml($_POST['EAE_Email']) ;


/* Build $message... */
$message='';
$message.= "TS_Name_:  " . stripandfilterhtml($_POST['TS_Name'])  ."<br />"; // Textfield
$message.= "TS_Email:  " . stripandfilterhtml($_POST['TS_Email'])  ."<br />"; // Textfield
$message.= "Tel_____:  " . stripandfilterhtml($_POST['TS_Tel'])  ."<br />"; // Textfield
$message.= "Enquiry_:  " . stripandfilterhtml($_POST['TS_Enquiry'])  ."<br />"; // Textfield

    $headers = "From: Form Feedback <john.ranson@eastanglianents.co.uk>";
    $headers = 'MIME-Version: 1.0' . "\\r\
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
";
    if(empty($errors))
    {
        $sent = mail( $to, $subject, $message, $headers ) ;
        if ( $sent ) {
            header( "Location: http://www.eastanglianentertainments.co.uk/pages/ts_thanks.html" );
            exit();
        } else {
            header( "Location: " . $_SERVER["PHP_SELF"] . "?email_failed" );
            exit();
        }
    }
}

htmlhead();

if ( isset( $_GET["thankyou"] ) ) {

    ?>Registration Received<?php
}

if ( isset( $_GET["email_failed"] ) ) {

    ?>There has been an issue with your registration, please try again. If the problem persists, please inform us through the contact webmaster link on the home page.<?php
}

if(!empty($errors))
print "Please check these errors:<br />". implode("<br />" , $errors);

foot();

?>