简体   繁体   中英

I am getting PhpMailer sending error

I am trying to send mail in Gmail using phpmailer from my websites contact form. And getting this error :

Parse error: syntax error, unexpected '@' in /home/dindaelectronics/public_html/mailer.php on line 19

Here is the PHP code, in which in line 19 has the error (which I am unable to understand).

<?php

if(isset($_POST['email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$mail             = new PHPMailer();
$body             = file_get_contents('contact.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->Host       = "mail.yourdomain.com"; 
$mail->SMTPDebug  = 2; 
$mail->SMTPAuth   = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Port       = 26;
$mail->Username   = "yourname@yourdomain";  <--  **Line 19 which is indicating error**

$mail->Password   = "yourpassword";


$email_to = "debnil2014@gmail.com";

$email_subject = "Checking PHP Mailer";





function died($error) {

// your error code can go here

echo "We are very sorry, but there were error(s) found with the form you submitted. ";

echo "These errors appear below.<br /><br />";

echo $error."<br /><br />";

echo "Please go back and fix these errors.<br /><br />";

die();

}



// validation expected data exists

if(!isset($_POST['name']) ||

!isset($_POST['address']) ||

!isset($_POST['email']) ||

!isset($_POST['mobile']) ||

!isset($_POST['subject']) ||

!isset($_POST['query'])) {

died('We are sorry, but there appears to be a problem with the form you submitted.');       

}



$first_name = $_POST['name']; // required

$last_name = $_POST['address']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['mobile']; // not required

$comments = $_POST['subject']; // required

$comments = $_POST['query']; // required


$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}



$email_message .= "Name: ".clean_string($first_name)."\n";

$email_message .= "Address: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Mobile: ".clean_string($telephone)."\n";

$email_message .= "Subject: ".clean_string($comments)."\n";

$email_message .= "Query: ".clean_string($comments)."\n";



// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->







<?php

}

?>

and Here is the HTML form...

<form name="contactform" method="post" action="mailer.php">
                            <input type="text" name="name" placeholder="Your Name" required>
                            <textarea name="address" placeholder="Address" required></textarea>
                            <input name="email" type="text" placeholder="Email id" required>
                            <input name="mobile" type="text" placeholder="Mobile No." required>
                            <input name="subject" type="text" placeholder="Subject" required>
                            <textarea name="query" placeholder="Query" required></textarea>
                            <div class="clear"></div>
                            <input type="submit" name="submit" value="SUBMIT">
                    </form>

So can anybody solve this?

From your question, if you are trying to send through GMail then you should be configuring using GMail's SMTP parameters but if using your webmail then it should be your host's SMTP parameters .

$mail->Host       = "mail.yourdomain.com"; 
$mail->SMTPDebug  = 2; //Change this to "true" to also see your errors
$mail->SMTPAuth   = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Port       = 26;
$mail->Username   = "yourname@yourdomain";  <--  **Line 19 which is indicating error**

$mail->Password   = "yourpassword";

Another thing is, [eregi_replace][1] is deprecated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM