LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-20-2013, 03:28 PM   #1
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Sending mail via PHP


Hey Gang,

Okay, this may well be a noob question, but, I'm learning PHP and...need to send a verification mail, perhaps a snippet

Code:
$file_handle = fopen("Media/Pages/Welcome.html", "r");
while (!feof($file_handle)) 
{
$line = fgets($file_handle);
if(stripos($line, "%fn%", 0)>0)
{
$line = str_ireplace("%fn%", $firstname, $line);
}
$message = $message . $line;					   
// just out of the stuff that kills cats :)
print $line;
}
fclose($file_handle);
// the actual mailer is here, the message was assembled
if (mail($who, $subject, $message)) 
{
   echo("<p>Message successfully sent!</p>");
} 
else 
{
echo("<p>Message delivery failed...</p>");
}
Now, some things:
- the mail does not arrive, so, I miss something. The install is a vanilla Debian server (no X, of course) with UFW and SendMail installed
- the print statement does not show the HTML, could it be that this gets stripped?
- I did a scan against te server, though the firewall rules state that port 25 is allowed
- zenmap states that port 25 is closed, though...

Any help wopuld gladly be taken...thanks

Thor
 
Old 05-21-2013, 03:02 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,419

Rep: Reputation: 99
1.
If you want to send an html mail you have to set the content type in the header accordingly:
$header = "From: someo@yxc.com"\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable";
mail($recipient, $subject, $mail_body, $header);

2.
You havn't set a "From"-header neither.

Is the script run from command line or from a web application? Is there something in the error logs?
 
Old 05-21-2013, 05:18 AM   #3
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Hi!

Thanks for this input...I knew I missed something. Usually, I do this on hosted (and hence fully configured) servers, this is the first time on a private server...

Quote:
Is the script run from command line or from a web application? Is there something in the error logs?
The script runs in a PHP file that is included in the calling page, and I have not seen any errors, but I looked in the apache error logs, since this is web based, now I realise I may have to look elsewhere

I'll correct the thing according to your suggestions and see where the "eagle lands"

Thanks!

Thor

Edit - I found the error log /var/log/mail.log, all the way down it says "no poute to host"...figuring this one out, but...I've gotta get to work now, so, I'll have to let this be, for now...

Last edited by Thor_2.0; 05-21-2013 at 05:45 AM.
 
Old 05-21-2013, 05:37 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 549

Rep: Reputation: 113Reputation: 113
This is simple php mail.
Code:
<?php
echo "Mail has been sent successfully !!!";
$to      = 'rootl@linux.com'; // Put recipients e-mail address here. 
$subject = 'subject line - Test mail for php';
$message = 'This is a Test mail. 
The second line of the mail.
This is a three line test mail';
$headers = 'From: no-reply@splendornet.com' . "\n" . // Put sender's e-mail address here.
        'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Try this, if it has been sent successfully, you can add if-else condition in it like your mail.
 
1 members found this post helpful.
Old 05-21-2013, 04:13 PM   #5
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
| eklavya - THANKS!!! Piecing stuff together without knowing where to end up it a challenge, this snippet should help me on, that and an eye on the error logs, if they still come into the picure!!
By the way, I noticed you landed here recently, welcome to the forum! Glad to have you around!!
 
Old 05-22-2013, 04:27 AM   #6
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Okay, so, I tested eklavya's snippet and this did not send the mail, or in other words: it sent the mail, just that the mail was not delivered......glossing over the log (the error log, mind you) I saw a "connection refused" - and I wonder: is there a mail server in place? Guess not - so, I guess now, is to select a mail SERVER, sendmail is an MTA and not a server...heh, very instructive all of this...

Thor
 
Old 05-22-2013, 04:45 AM   #7
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 314

Rep: Reputation: 77
You can use sendmail to send emails, you just have to configure it properly.
 
Old 05-22-2013, 04:45 AM   #8
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,419

Rep: Reputation: 99
I'm not an expert in this but I think the receiving mail server refuses the connection. You can tell PHP to use an SMTP server for sending mails instead of sendmail. So maybe you want to setup an SMTP server on that box?
 
Old 05-22-2013, 04:51 AM   #9
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 314

Rep: Reputation: 77
Sendmail is an smtp server
 
Old 05-22-2013, 05:25 AM   #10
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
...and I entered the IP adress of the SMTP server of my provider, just last nite (about 3-ish it hit me in the face: I never configured anything...so I'd have to give it the IP adress of the SMTP server I want to use, of course, I still get a connection refused on 127.0.0.1...and that's weird, since it's not set up that way, that I know of...

Eh...gotta be off to work now, so, some rest-n-meditation is called for

Thor
 
Old 05-22-2013, 06:22 AM   #11
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,419

Rep: Reputation: 99
sendmail is a mail transfer agent that is pretty difficult to configure as far as i know. Thus an smtp server like postfix might be a good option.
 
1 members found this post helpful.
Old 05-22-2013, 12:34 PM   #12
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,614

Rep: Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415Reputation: 1415
1. Have you looked at Swiftmailer?

2. Do you really need to set up your own mail server? If you have access to an external one (e.g. Sendgrid or GMail) then you can relay mail through that.
 
1 members found this post helpful.
Old 05-23-2013, 03:50 AM   #13
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,203
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
@ j-ray : umm, so, it's not really MY impression but this thing really is a ... "challenge" to put it mildly. Lemme iinstall Postfix instead, then...

@ dugan : ... or Swiftmailer, at this point I'm not biass., since I'm asking the question, thus dont know....
And, yes, I need to send mails. This is a mailback server and at a later point, I'd like to offer free webmail to the users as well, so, it's best to set things up NOW and get it "over with" so to speak...
This server (a Dell T105) is either a test case or should one day be fit to face the "big bad Net" ...
 
  


Reply

Tags
firewall, ip ports, sendmail


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sending mail with php mailer call_krushna Linux - Software 6 06-07-2012 05:56 AM
Delay in sending mail from php abhihebbar Linux - Server 4 11-23-2011 09:32 AM
Sending E-Mail From a Different Server Using PHP stevec Programming 6 08-02-2011 12:53 PM
sending mail in php to Israel mail servers Four Programming 4 10-23-2009 10:37 AM
Sending mail with PHP and sendmail. davemar Linux - General 10 06-11-2004 04:48 PM


All times are GMT -5. The time now is 03:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration