My mail script isn't working

the mail() function

Moderators: macek, egami, gesf

theboyholty
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Apr 04, 2011 11:34 am

My mail script isn't working

Postby theboyholty » Fri Apr 04, 2014 11:39 pm

Hi. I'm fairly new to php and my send mail script isn't working. When I run the code below, I get the message 'Email sent successfully', but then I don't receive anything.

Anyone know what I'm doing wrong?

Code: Select all

<?php
$email_to = 'info@cinic.co.uk';
$name = $_REQUEST['YourName'];
$email_from = $_REQUEST['EmailAddress'];
$message = $_REQUEST['Comment'];
$email_subject = 'Feedback from cinic.co.uk';
$headers = 'Content-Type: text/html; charset=utf-8';

echo '<br />';
echo "Your Name: " . $name;
echo '<br />';
echo "Your Email Address: " . $email_from;
echo '<br />';
echo "Your Comments: " . $message;
echo '<br />';

ini_set("sendmail_from", $email_from);
$send = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);

if (!$send) {
   echo 'Email did not send';
} else {
   echo 'Email sent successfully';
}
?>

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: My mail script isn't working

Postby seandisanti » Sat Apr 05, 2014 10:22 am

using mail() is dependent on correctly configured sendmail binary. I personally use phpmailer to send via external mail server like gmail. here's a quick example http://stackoverflow.com/questions/1604 ... php-mailer

theboyholty
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Apr 04, 2011 11:34 am

Re: My mail script isn't working

Postby theboyholty » Mon Apr 14, 2014 4:56 am

I finally got my script working after much messing about with it. It turns out the problem was with echoing the variables as shown below:

Code: Select all

$email_to = 'info@cinic.co.uk';
$email_from = 'info@cinic.co.uk';
$email_subject = 'Feedback from Cinic website';
$message = $_REQUEST["YourName"];
$message .= ' [';
$message .= $_REQUEST["YourEmailAddress"];
$message .= '] Says: ';
$message .= $_REQUEST["YourMessage"];
$headers = "From: ".$email_from."\r\n"."Reply-To: ".$email_from."\r\n"."X-Mailer: PHP/".phpversion();

echo $email_to;
echo $email_from;
echo $email_subject;

$send = @mail($email_to, $email_subject, $message, $headers);


When I took the echo $email_to; etc lines out, it worked fine, but with them in the whole page failed.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: My mail script isn't working

Postby seandisanti » Mon Apr 14, 2014 8:14 am

that makes sense, thanks for posting the fix


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest