ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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?
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.
<?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.
| 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!!
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...
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?
...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
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.
@ 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" ...
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?");'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.