PeterPans Posted October 22, 2012 Share Posted October 22, 2012 Hello, I wrote an emailer script to add to my web site. The code seems to be fine and the variables seem to parse, but still I can not send an e-mail. Could you please tell me what am I doing wrong? I think the error is somewhere in the sendEmail function, but a second opinion is always better to have. The code is: <?php class emailer { private $sender; private $subject; private $body; function __construct($sender) { $this->sender = $sender; //$this->recipient = $recipient; } function addSender($sender){ $this->sender = $sender; } function addSubject($subject){ $this->subject = $subject; } function addBody($body){ $this->body = $body; } function sendEmail(){ $this->recipient = 'test@mail.com'; $this->result = mail($this->recipient, $this->subject, $this->body, "From: {$this->sender} \r\n"); if (!$this->result){ echo $this->result; echo $this->recipient; echo $this->sender; echo $this->subject; echo $this->body; echo "Error"; } else{ echo "e-mail was sent."; } } } ?> Thank you in advance. PS. when I execute the script, I get as output "Error", and I also get the data stored in the variables (recipient,sender,subject, and body). Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 22, 2012 Share Posted October 22, 2012 (edited) Hi I had a quick look at your code and made a couple of little changes, I've tested this on my server and it seems to work fine now. Give it a try and let me know if it works for you... <?php error_reporting(E_ALL); class Emailer { private $sender; private $recipient; private $subject; private $body; public function __construct($sender) { $this->sender = $sender; } public function setSubject($subject) { $this->subject = $subject; } public function setRecipient($recipient) { $this->recipient = $recipient; } public function setBody($body) { $this->body = $body; } public function sendEmail() { $result = mail($this->recipient, $this->subject, $this->body, "From: $this->sender \r\n"); if (!$result) { echo "Failed to send email!"; } else { echo "Email sent successfully!"; } } } $sender = "you@your_email"; $recipient = "test@mail.com"; $subject = "test email"; $body = "test email"; $myEmail = new Emailer($sender); $myEmail->setSubject($subject); $myEmail->setRecipient($recipient); $myEmail->setBody($body); $myEmail->sendEmail(); ?> Edited October 22, 2012 by jordan21 Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 No it doesn't.. I get Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in /bla/bla/class.emailer.php on line 3 If i try to execute it again without the error_reporting(E_ALL); I can see that the data are getting stored in the variables but the e-mail is not being sent.. Can you show me how you have set up your [mail function] in your php.ini file? I follow the runtime configuration from the php manual in order to configure mine, but still i can't send e-mails.. :/ Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 That's strange... I've just tested the code again on my server with error reporting turned on and I haven't got any errors or warnings and the email was sent fine, did you copy and paste the code I posted? in my php.ini I have SMTP set to localhost and SMTP_PORT set to 25, are you sure that you have the correct SMTP server and port settings for your web host in your php.ini? and also, are you setting the from address as an email address that actually exists on your domain (e.g you@yourdomain.com)? Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 SMTP and SMTP_PORT are the same.. the from address is inactive.. Am I supposed to put my e-mail address there? though why should I put my e-mail address? the e-mails should be sent to my e-mail address not from my e-mail address.. I am making this form for other to be able to send me e-mails.. not for me to send e-mails.. Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) I am confused.. and now I am 100% sure that there is something wrong with my configuration.. If i use your script.. I get "Failed to send e-mail".. Edited October 23, 2012 by PeterPans Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) and thats how I have my [mail function] set up in my php.ini [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = "From:" example@mail.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path = /usr/sbin/sendmail -t -i ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters = ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = 0 ; The path to a log file that will log all mail() calls. Log entries include ; the full path of the script, line number, To address and headers. mail.log = /var/log/mail.log Edited October 23, 2012 by PeterPans Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 Sorry for the confusion, I think I miss read your original post, I thought that you wanted to create a form on your website that will let you send emails to people. Now that I've re read your posts I think what you want is a contact form so that visitors to your site can fill out a contact form which will send an email to you, is that correct? if yes, then I've modified the code a little bit and it should work for you now: <?php error_reporting(E_ALL); class Emailer { private $sender; private $recipient; private $subject; private $body; public function __construct($sender) { $this->sender = $sender; } public function setSubject($subject) { $this->subject = $subject; } public function setRecipient($recipient) { $this->recipient = $recipient; } public function setBody($body) { $this->body = $body; } public function sendEmail() { $result = mail($this->recipient, $this->subject, $this->body, "From: $this->sender \r\n"); if (!$result) { echo "Failed to send email!"; } else { echo "Email sent successfully!"; } } } $sender = htmlspecialchars($_POST['from_email']); $recipient = "you@youremail.com"; $subject = htmlspecialchars($_POST['subject']); $body = htmlspecialchars($_POST['body']); $myEmail = new Emailer($sender); $myEmail->setSubject($subject); $myEmail->setRecipient($recipient); $myEmail->setBody($body); $myEmail->sendEmail(); ?> Just make sure that the input field names that you use in your contact form match what's at the bottom of the script and that the form method is set to "post". If your still having problems getting it working then let me know. Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) yes thats what i want to do.. i tried the new script you posted.. still it does not send e-mails.. I am pretty sure its a configuration issue.. Would you mind posting the [mail function] of your php.ini file? So, I can compare it with mine.. Thank you for helping me out by the way.. Edited October 23, 2012 by PeterPans Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 No problem I'm on a shared host, so I've not got direct access to php.ini, but when I do phpinfo(); it shows I have the following mail settings: sendmail_path = /usr/local/bin/sendmail (this varies depending on your server setup) sendmail_from = not set SMTP = localhost smtp_port = 25 Are you getting any error messages or warnings at all when you run the script with error reporting on, or is it just saying "Failed to send email!"? Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 no error messages, nothing.. just "Failed to send email!".. :/ Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 Hmm... strange :-\ are you testing this on your own dev environment or on your web host? Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 on my own environment (localhost), the thing is that others (and you) have tested my script as well.. to check if it is working and it does.. so i know that the script works fine.. what i dont know is what i have done wrong on the php.ini file.. because I am pretty sure that thats where the problem is.. :/ Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 are you running on a windows or a linux system? and do you actually have an smtp server like sendmail installed? Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 im running linux ubuntu.. and I have no idea how to set up those.. do these come with the installation of Apache? Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 It sounds like you don't have any SMTP server installed on your system, which is why the script isn't working for you. Your best bet is to install postfix. If you go to this link https://help.ubuntu.com/community/Postfix it talks you through installing postfix (SMTP server) on ubuntu step by step, and it shows you how to configure it correctly aswell. After you've got this installed the script should (in theory!) work fine. Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) I have actually installed sendmail.. not sure If i should install Postfix as well.. or not sure which one to use.. I used this http://www.yolinux.c...S/Sendmail.html but still it doesnt work.. :/ Edited October 23, 2012 by PeterPans Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 http://www.mydigitallife.info/how-to-send-an-email-mail-message-from-linux-command-line-shell/ Try sending an email from the command line and see if that works. If it does then I'm assuming it's something in your php.ini that hasn't been set right thats stopping the mail() function from sending emails, if it doesn't then it could be a problem with the way that sendmail has been configured... Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 Also, the php manual saya that no installation of any kind of mail server is required because these things come with the PHP core files.. http://gr2.php.net/manual/en/mail.installation.php Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 If you look at the requirements page on the php manual, it says that php needs to have access to the sendmail binary on your system in order for the mail() function to work properly. when it says that there are no installations required, it means that you do not need to install any additional modules as the mail() function itself is part of the php core. http://gr2.php.net/manual/en/mail.requirements.php Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) yes you are right.. but the thing is that every required module is installed... sendmail, mailutils.. etc.. :/ Also, I am trying to send e-mail from the terminal, but for some reason I have not moved further after typing the message.. im hitting Ctrl + D but nothing happens.. wtf????!!!!!! Edited October 23, 2012 by PeterPans Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 ok.. my system must be badly configured for some reason.. Mailutils is installed and working, but when i am done writing the body of the e-mail.. I press ctrl + D to proceed to the sending of the e-mail.. but nothing happens.. Quote Link to comment Share on other sites More sharing options...
jordan21 Posted October 23, 2012 Share Posted October 23, 2012 I've just found this http://stackoverflow.com/questions/4963688/how-to-send-email-from-php-without-smtp-server-installed on stack overflow, it explains how to use phpmailer, it looks like it could be a much easier option for you than having to mess about getting sendmail + mailutils configured on your own system. All you need is a gmail account and you can send email using google's smtp servers. You can get phpmailer here http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list Quote Link to comment Share on other sites More sharing options...
PeterPans Posted October 23, 2012 Author Share Posted October 23, 2012 oh cool! thank you! I will have a look at that! Quote Link to comment Share on other sites More sharing options...
shaddowman Posted October 23, 2012 Share Posted October 23, 2012 Some ISP does not permit you to send emails using default port 25 try to change that (php.ini) to port 26. Restart the web server and test it again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.