Engr Atiq
Engr Atiq

Reputation: 163

PHP mail function returns false despite all the settings

I am a newbie in PHP, so all i know is actually from the forums. These are the settings i made in my php.ini file

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xamppnew\sendmail\sendmail.exe\" -t"
;sendmail_path = "C:\xamppnew\mailtodisk\mailtodisk.exe"

These are the changes made in sendmail.ini file

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=************
[email protected]

And here is the code I am using to send the mail

$to = "[email protected]";
$myemail = "[email protected]";    
$email_subject = "Contact form submission: $name";
$email_body = "my message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
header('Location: ../index-alt2.html?t="done successfully"');

Upvotes: 0

Views: 1845

Answers (1)

CloudBranch
CloudBranch

Reputation: 1604

I would recommend using PHPMailer to send email from PHP. Here's the steps to accomplish this.

  1. Go to the Github repository.
  2. Download the ZIP.
  3. Extract it in your public_html directory.
  4. include '/path/to/PHPMailer/PHPMailerAutoload.php'; at the top of your PHP script.
  5. Get the values from the HTML form like you normally would.

Here's an example...

index.html

<form action="index.php" method="post">
    <input type="email" name="email">
    <input type="text" name="name">
    <input type="text" name="subject">
    <input type="text" name="message">
</form>

index.php

include '/path/to/PHPMailer/PHPMailerAutoload.php';

$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, "ssl" also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom('your email', 'your name'); // from
$mail->addAddress($email, $name); // to
$mail->isHTML(true); // if html

$mail->Subject = $subject;
$mail->Body = $message; //HTML

if($mail->send()){
    echo 'Mail sent!';
}
else {
    echo 'Mail failed!';
}

Upvotes: 1

Related Questions