-2

I have tried sending email from a basic html form by php mail() function and It has given me false return

Code:
<?php 
if(isset($_POST['submit'])){
        $title = $_POST['title'];
        $name = $_POST['first_name'];
        $name1 = $_POST['last_name']; 
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $company=$_POST['lead_object'];
        $skype= $_POST['skype_id'];
        $to = 'himanshu@webkidukan.com';
        $subject = 'Advertiser Form Details';
        $from = 'ssing648@gmail.com';

        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        // Create email headers
        $headers .= 'From: '.$from."\r\n".
            'Reply-To: '.$from."\r\n" .
            'X-Mailer: PHP/' . phpversion();

        // Compose a simple HTML email message
        $message = '<html><body>';
        $message .= '<h1 style="color:#f40;">Hi Sir</h1>';
        $message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
        $message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
        $message .= '</body></html>';
        $t = mail($to, $from, $message, $headers,$subject);
        var_dump($t);exit;
        if ($t) {
            echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";

        }else{
           echo "Failed to send email. Please try again later";
        }

    echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";    

    echo "<script> window.location.href = 'advertiser.php';</script>";  
}

 ?>

I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.

Himanshu
  • 43
  • 8

1 Answers1

0

There are some unwanted PHP tags while appending message. try like this

$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';
Aroon
  • 991
  • 2
  • 15
  • 34
  • Okay..I will try – Himanshu Nov 28 '19 at 06:40
  • I am able to send the mail, now....But how can i call the PHP values in $message. $message = ''; $message .= '

    Hi Sir

    '; $message .= '

    Details of Advertiser Form

    '; $message .= '

    '.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'

    '; $message .= '';
    – Himanshu Nov 28 '19 at 09:07
  • 1
    @Himanshu `$message .= '

    '.$title.'`

    – kry Nov 28 '19 at 09:50
  • Cool. enjoy coding! – Aroon Nov 28 '19 at 09:56
  • If my answer helped you kindly accept it. else leave it – Aroon Nov 28 '19 at 10:08