LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sending mail via PHP (http://www.linuxquestions.org/questions/programming-9/sending-mail-via-php-4175462735/)

Thor_2.0 05-20-2013 03: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

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

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

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?

Thor_2.0 05-21-2013 05:18 AM

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

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

eklavya 05-21-2013 05:37 AM

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.

Thor_2.0 05-21-2013 04: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!!

Thor_2.0 05-22-2013 04: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...

Thor

Celyr 05-22-2013 04:45 AM

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

j-ray 05-22-2013 04: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 04:51 AM

Sendmail is an smtp server

Thor_2.0 05-22-2013 05: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 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 :D

Thor

j-ray 05-22-2013 06: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 12: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.

Thor_2.0 05-23-2013 03: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 12: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:

Code:

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


All times are GMT -5. The time now is 12:16 AM.