Jump to content

Recommended Posts

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).

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/
Share on other sites

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 by jordan21
Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387097
Share on other sites

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.. :/

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387157
Share on other sites

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)?

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387181
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387197
Share on other sites

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 by PeterPans
Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387201
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387220
Share on other sites

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 by PeterPans
Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387226
Share on other sites

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!"?

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387230
Share on other sites

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.. :/

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387248
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387260
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387264
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387270
Share on other sites

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 by PeterPans
Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387273
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/269786-php5-emailer-issue/#findComment-1387279
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.