LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
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
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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 ButterflyMelissa; 05-21-2013 at 05:45 AM.
 
Old 05-21-2013, 05:37 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
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
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
| 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
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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: 321

Rep: Reputation: 81
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, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
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: 321

Rep: Reputation: 81
Sendmail is an smtp server
 
Old 05-22-2013, 05:25 AM   #10
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
...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, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
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
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
@ 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" ...
 
Old 09-19-2014, 12:46 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Bumping this old thread to post a good answer. Someone might search it up.

Don't mess with an MTA for this.

Install either ssmtp or msmtp. Set ssmtp or msmtp to relay mail through the SMTP server you have access to. Then edit php.ini and set PHP's sendmail_path to either /path/to/msmtp -t for msmtp or /path/to/ssmtp -t for ssmtp.

Then test sending email from PHP:

Code:
php -r 'mail("username@provider.com", "testing from PHP", "Did this arrive?");'
 
  


Reply

Tags
firewall, ip ports, sendmail



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration