Chris
Chris

Reputation: 1939

sendmail.php needs some php work

I am having the hardest time to get a simple sendmail.php to work. My form html is

<form action=sendmail.php id=contact-form method=post>
    <p>
        <label for=cf_name>Name *</label>
        <input id=cf_name name=cf_name placeholder='Enter your name...' required=required title=Name type=text />
    </p>
    <p>
        <label for=cf_email>Email *</label>
        <input id=cf_email name=cf_email placeholder='Email address...' required=required     title='Email address' type=email />
    </p>
    <p>
        <label for=cf_subject>Subject *</label>
        <input id=cf_subject name=cf_subject placeholder='Specify subject...' required=required title=Subject type=text />
    </p>
    <p>
        <label for=cf_message>Message *</label>
        <textarea id=cf_message name=cf_message placeholder='Message text...' required=required rows=10 title='Message text'></textarea>
    </p>
    <p>
        <input type=submit value='Send message'/>
    </p>
</form>

And my mailer script is:

<?
    $cf_email = $_POST['cf_email'] ;
    $cf_message = $_POST['cf_message'] ;
    $cf_subject = $_POST['cf_subject'] ;
    $cf_name = $_POST['cf_name'] ;

    mail( "[email removed]", $cf_subject, $cf_message, $cf_name, $cf_email );
    print "Congratulations your email has been sent";
?>

Just want an email to to go to my email. When it appears in the inbox, the subject they typed is the one that I will see as the subject in my inbox. The from will be their name. The email it came from will be their email And the message inside will be the message they wrote in the form. Please help.

Upvotes: 0

Views: 297

Answers (2)

Lucas
Lucas

Reputation: 10646

You might want to look at the example in the PHP documentation.

Basically it looks like you aren't passing the arguments to mail() correctly.

From http://php.net/manual/en/function.mail.php it should be:

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Using example #4 as a reference from the PHP documentation you should be able to do this:

$cf_email = $_POST['cf_email'] ;
$cf_message = $_POST['cf_message'] ;
$cf_subject = $_POST['cf_subject'] ;
$cf_name = $_POST['cf_name'] ;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $cf_name . ' <' . $cf_email . '>' . "\r\n";

mail('[email protected]', $cf_subject, $cf_message, $headers);

But as Homer6 has said, I'd recommend using something like swiftmailer if you can.

Upvotes: 0

Homer6
Homer6

Reputation: 15159

I'd highly recommend using an email library like http://swiftmailer.org/

Sending emails is actually quite complex and there are a number of ways to do it wrong.

Also, if you're setting up a professional website, I'd recommend using a reliable smtp gateway, like http://sendgrid.com/ or gmail to mitigate your emails being picked up as spam.

Upvotes: 1

Related Questions