php Email code not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Narcud
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 3:43 am

php Email code not working

Post by Narcud »

Hi, I am using the following code to send an email from a Feedback.htm form
- when it is opened in the browser it then takes me to the Thanks.htm page as expected, but I never receive an email. There is no error message from the mail.php code below when it is ran
Can anyone suggest where the error may be ?

thanks
Nic

<?php

$address = "myaddress@hotmail.co.uk";
$firstname = $_POST['firstname'];
$secondname = $_POST['secondname'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$subject = "Feedback left by ".$name.".";
$message = "The following comment was left to you by ".$firstname." ".$secondname."(".$email.") on ".date("d/m/Y").".
".$comments.".";
$headers = "From: ".$email."";
@mail($address,$subject,$message,$headers);
header("Location:Thanks.htm");
?>
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: php Email code not working

Post by novice4eva »

don't do

Code: Select all

 
@mail($address,$subject,$message,$headers);/* RATHER JUST do mail() without @*/
 


as it will suppress any errors returned by that particular function. If that doesn't show up anything... I never had to correct it because there is always a system admin to help: but i think you have to make corrections in your PHP.ini file too, setting SMTP or something of that nature :crazy: . To enable error messaging add this at the top (you may already know this but then also)

Code: Select all

 
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
 
amit007
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 5:52 am

Re: php Email code not working

Post by amit007 »

You can use below function to send mail:

mail(to,subject,message,headers,parameters);
Narcud
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 3:43 am

Re: php Email code not working

Post by Narcud »

Hi thanks for the repsonse. I have updated my code to :

<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
$address = "myaddress@hotmail.co.uk";
$firstname = $_POST['firstname'];
$secondname = $_POST['secondname'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$subject = "Feedback left by ".$firstname.".";
$message = "The following comment was left to you by ".$firstname." ".$secondname."(".$email.") on ".date("d/m/Y").".".$comments.".";
$headers = "From: ".$email."";
mail($address,$subject,$message,$headers);
header("Location:Thanks.htm");
?>
and get the following errors


Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Inetpub\vhosts\studentweb.stockport.ac.uk\subdomains\astudent\httpdocs\mail.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\studentweb.stockport.ac.uk\subdomains\astudent\httpdocs\mail.php:9) in C:\Inetpub\vhosts\studentweb.stockport.ac.uk\subdomains\astudent\httpdocs\mail.php on line 13


any ideas ?

Thanks

Nic
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

Re: php Email code not working

Post by waqas_punjabian »

Buddy,

You are testing your email script on your localhost, where you have not configured the mailing server (smtp, pop3).
When you will run this code on some online host, this will work fine.


Waqas
Post Reply