-   Programming (
-   -   Sending mail via PHP (

ButterflyMelissa 05-20-2013 04:28 PM

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


$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;
// the actual mailer is here, the message was assembled
if (mail($who, $subject, $message))
  echo("<p>Message successfully sent!</p>");
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


j-ray 05-21-2013 04:02 AM

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

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?

ButterflyMelissa 05-21-2013 06:18 AM


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


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 :doh:

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



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

eklavya 05-21-2013 06:37 AM

This is simple php mail.

echo "Mail has been sent successfully !!!";
$to      = ''; // 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:' . "\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.

ButterflyMelissa 05-21-2013 05:13 PM

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

ButterflyMelissa 05-22-2013 05:27 AM

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 :doh: - so, I guess now, is to select a mail SERVER, sendmail is an MTA and not a server...heh, very instructive all of this...


Celyr 05-22-2013 05:45 AM

You can use sendmail to send emails, you just have to configure it properly.

j-ray 05-22-2013 05:45 AM

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?

Celyr 05-22-2013 05:51 AM

Sendmail is an smtp server

ButterflyMelissa 05-22-2013 06:25 AM

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


j-ray 05-22-2013 07:22 AM

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.

dugan 05-22-2013 01:34 PM

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.

ButterflyMelissa 05-23-2013 04:50 AM

@ 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.... :D
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" ...

dugan 09-19-2014 01:46 PM

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:


php -r 'mail("", "testing from PHP", "Did this arrive?");'

All times are GMT -5. The time now is 11:56 PM.