user1939243
user1939243

Reputation:

PHP Mail form adjustment

Is this code enough if user want to send email to my webmail? Or I need to make changes?

<?php
$mail = $_POST['mail'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$text = $_POST['text'];

  $to = "[email protected]";

 $message =" You received  a mail from ".$name;
 $message .=" Text of the message : ".$text;

 if(mail($to, $subject,$message)){
echo "Your message was sent successfully.";
} 
else{ 
echo "there's some errors to send the mail, verify your server options";

}

?>

Upvotes: 0

Views: 247

Answers (4)

THE ONLY ONE
THE ONLY ONE

Reputation: 2190

No this code is not enough if you want to send email by setting the content-type of your type in header and the receiver of mail must come to know sender of the mail. The code is below:

  $to = "[email protected]";

 $message =" You received  a mail from ".$name;
 $message .=" Text of the message : ".$text;
 $headers = "Content-Type: text/html; charset=iso-8859-1\r\n"; 
 $headers = "From: ". Please enter the name of sender . "\r\n";

 if(mail($to, $subject,$message,$headers)){
echo "Your message was sent successfully.";
} 
else{ 
echo "there's some errors to send the mail, verify your server options";

}

Upvotes: -1

Guy Nakash
Guy Nakash

Reputation: 36

I suggest adding a header to the email with encoding (UTF8), encode the subject line so you won't get Gibberish (if you use other non-Latin characters for example) and handle basic events, success or not.

<?php 
$name = $_POST['name'];
$text = $_POST['text'];
$from = $_POST['mail'];
$to = "[email protected]";
$subject = "=?utf-8?B?".base64_encode($_POST['subject'])."?=";
$message = " You received  a mail from ".$name;
$message .= " Text of the message : ".$text;

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "To: <$to>\r\n";
$headers .= "From: $name <$from>\r\n";    

if (mail($to,$subject,$message,$headers)) {
    // Do something if the email is sent
} else {
    // Do something if there's an error
}
?>

Upvotes: 1

Indian
Indian

Reputation: 645

This code surely work for you.

<?php 
    $to = '[email protected]';
    $subject = "Your Subject";
    $message ="<html><body>
    <div>Here Write Your Message</div>
    </body></html>";

    $header='';
    $header .= 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $header .= 'From: [email protected]'. "\r\n";

    mail($to,$subject,$message,$header);
?>

Note : Mail function only work in live server Not in Local server.

Upvotes: 1

Arun Unnikrishnan
Arun Unnikrishnan

Reputation: 2349

This is okay for simple mail. But mail() function is not suitable for larger volumes of email in a loop.The function opens and closes an SMTP socket for each email, which is not very efficient. For sending large amounts of email, see the PEAR::Mail, and PEAR::Mail_Queue packages.

Upvotes: 0

Related Questions