Relaying mail with PHP

Relaying mail with PHP

Sending mail through a mail server that requires authentication (A username and password).

This article is when you do NOT want to use your server’s MTA with PHP, and you want to send through an MTA that requieres authentication (A remote mail server)

When sending email from your own MTA with sendmail, PHP can use the mail() function to send such mail by smply passing the mail function some arguments, Even when the MTA is on a different computer that allows you to send mail (By validating your IP), changes can be made to PHP.INI to make the mail function work as needed.

But when you want to automate sending emails from something like a GMAIL account or a Godaddy Mail Server, things are different, You will need to connect to the SMTP server and ask it to relay your mail, for that the server will ask you for Authentication credentials, this tutorial will show you how to send email from a different email provider.

So, to begin with, you will need to download this script that i originally obtained from the open source GPL Coppermine Project, they in turn took the code from the GPL PHPMailer, a small change is required to make this file work.

Click here to download the mail slass scripts

What this script is about to do is exactly what your Outlook or Thunderbird do when you manually send a message from them, this is not a magic solution, the Gmail sending limits still apply, and all other big providers have limits even when you are a paying customer. mail providers need to protect there servers from being labled as spammers, some rules need to be in place.

Also, before you start, you should look carefully at the limits, not only is there a send limit, a large number of bounces will also lock your account in the case of Gmail and others.

In this tutorial we will use the functions in the PHPMailer class “http://phpmailer.sourceforge.net/”, it is free GPL software.

but rather than downloading it from PHPMailer’s website, we will take it from within the Coppermine Gallery Software, the file we need is include/mailer.inc.php

a few modifications need to be done, First seach for the string “connect to the smtp server” and add the following lines above it

$host = “ssl://smtp.gmail.com”;
$port = 465;

then search for “function ServerVar”, and right after the opening “function ServerVar {” and before “$superCage = Inspekt::makeSuperCage();” add

return ”;

Now, our mailer script should be ready !

Now, create a new PHP file (sendmymail.php), and within that paste the following code, Fix all names, credentials and email addresses.

require 'mailer.inc.php';
$mail = new cpg_PHPMailer();//this is the name of the class in coppermine
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // If you want to add attachments !
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // If you want to add attachments
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body.
if(!$mail->Send())
{
	print "Mailer Error: " . $mail->ErrorInfo;
}
else
{
	print "Message has been sent";
}

?>

Adding SPF records

Adding SPF records

Note: Microsoft Sender ID is in many ways similar to SPF, and in most cases, SPF and SenderID are compaible, so by making an SPF record, you are also creating a Microsoft SenderID record, that sometimes causes trouble when the envelop sender is not the same as the From address !

Here, i will start by showing you a list of SPF records, and what they mean to mail servers, You can use those SPF records or compose your own, as the syntax is very simple really.

If you do not want to take chances, you can use the SPF generator at openspf.org

For example, the most common SPF record is

"v=spf1 a mx ~all"

–Explaining the line above, v=spf1 means that this is an SPF TXT record, it is common to all SPF records,
–followed by an a that means “any computers in the A record fields of the domain’s DNS are allwed to send email, or more simply put, to allow your web server (where you may run PHP scripts that send email for example) to send emails on the domain’s behalf
— Followed by an mx which means all servers mentioned in DNS as mail servers for this domain, are also allowed to send email on this domains behalf.
— ~all means that this loosly describes all servers that can send email on this domain’s behalf (softfail), while a -all would mean that this strictly describes all servers that send mail on our domain’s behalf (fail), and a ?all means that this describes some of the domains allowed to send email on our domain’s behalf, notice that ~ is not – from 1 and 2, ~ is loosly, – is strictly.

But for our examples sake, let us look at a longer, expanded SPF record and see what the extra fields mean

"v=spf1 a mx a:xxx.xxx.xxx.xxx mx:mailsrv2.example.net ip4:yyy.yyy.yyy.yyy/24 ip4:xxx.xxx.xxx.xxx include:mailsenders.com ~all"

In the example above, the section that reads mx:mailsrv2.example.NET is a mail server that is also allowed to send email on example.COM’s behalf

a:xxx.xxx.xxx.xxx where the masked charracters are an IP address means that the server or mail sender with the IP address mentioned can also send email, you can add as many such sections separated by a space as you wish to allow many computers to send mail on the domain name’s behalf

Google mail (gmail) sending limits

google mail has the following sending limits

GMAIL accounts…

google apps account limits

The links above come down to…

1- GMAIL accounts

500 Emails per day, 500 on the receiving end, or simply, 500 recipients of 500 messages

2- Google apps accounts

500 recipients.. 100 a time when using SMTP

But, what is not stated is that a large number of bounced messages, or email messages that look like spam will also get your account disabled, i found that out on a forum on the internet,

In general, you can send email with PHP through Gmail’s servers for notification and the like, but it is best if you triple check the data before it goes to your account !

Click here to see how to send email from PHP through your Google mail “Gmail” account.

Common name example.com is already present in a current certificate

Four days of godaddy SSL hell (starfield technologies certificate)

So, i am not writing this to mock godaddy or godaddy resellers or support, this is just a problem that you need to understand before you call godaddy (or any of their resellers) simply to save time and not to have to wait for 4 days like i did

When i submit my security signing request (csr file) to godaddy or wild west domains, the error i get reads

Common name example.com is already present in a current certificate.

The reason to this is that someone (probably you or a previous owner) already issued a certificate for that domain from another account.

SOLUTION: Certificate, or even expired certificate must be REVOKED, cancelled is not good enough, the magic word is REVOKED, when the certificate expires, you can not revoke it, you must contact support and tell them that you need to revoke it by email.

So, i have not taken the time to organize the text below this line yet, if you are arguing about something in an effort to reduce your wait time, see below for whatever you need, but again, i did not refine any text below this line or organized it or even checked that it is correct.

———————————————————————-

UPDATE: Godaddy wrong again, when i get the time i will listen to the recorded conversation (because my phone auto records all conversations) and tell you exactly what you need to do to not rely on the faulty godaddy manuals, in short this is what happened (as i remember it is close to this)

So, here is what my conversations with godaddy comes down to, not accurately, but in short, what it comes down to (for my reference, the file is godaddy ssl Voice-0003.amr)

But as i start to get skeptical about this resolving itself in a few hours, i will call jet (the very helpful customer care representative) again and see if anything can be done.

Godaddy (Jet): After canceling the certificate, you need to wait for three days
Me: No, i am sure we have to revoke it, and since it is expired, i can not revoke it
Godaddy (Jet): No you are mistaken, after cancelling, we wait for three days then put in a new request
Me: Ok i will wait
I wait for 2 days, then call again as my website is down
Me: are you sure that within 3 days the system will do cleanup, if the job runs once every three days, 2 days increases the odds of what i was saying being right, can you please double check ? my website has been down for two days
Godaddy: no need to check, there is nothing we can do
And after 3 days of still no luck, i call again
Me: hi, i have waited for 3 days
Godaddy (denis): yes sir, for a certificate to get cleared from the system it needs to be revoked, i will have them send you an email so we can revoke it by email.
me: Seriously, that’s what i said 3 days ago
Godaddy (denis): I wonder why they did not do that on the first day
Me: thanx anyways

installing proper SSL on apache

You are looking for A-Z instructions, what i am doing here is to show you how to install a godaddy or starfield certificate to a website on apache server on a debian system, if you want the instructions to issue the certificate yourself (self signed certificate), i have covered that in another post, you can adopt this to the system of your choice, here i will explain what i am doing too so that you can adapt it to other systems

Note that you need a dedicated IP address for every website / certificate.
I have apache already installed on debian squeeze and running a website with no SSL

1- Before we begin, you may want to execute

apt-get update

2- Install openSSL, on debian this is done with

apt-get install openssl ssl-cert

3-Create a directory for the keys

mkdir /etc/apache2/sslkeys

4- Creating a PRIVATE key (Give to no one)

Before executing this command
You will be asked to chose a password and enter it twice, please keep this password on a paper close to you since we will need this password to decrypt this key in the following steps, this password is important during this process, no longer important after that.

openssl genrsa -des3 -out /etc/apache2/sslkeys/server.key 2048

5- Create a signing request to give to godaddy or starfieldtech
Before executing this command, remember that from the questions you will face, the only one that is TEHNICALLY IMPORTANT IS to use the common name example.com (not www.example.com), unless it is a subdomain other than www you can use subname.example.com, all other fields you should answer as you would like them to appear to people, but the certificate will not work with an incorrect common name

 openssl req -new -key /etc/apache2/sslkeys/server.key -out /etc/apache2/sslkeys/server.csr

NOTE: we could have created a signing request and a private key in one go with

openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr

But we chose to not do that because this tutorial aims to show you the exact steps and what they do

6- Now, we have a secure signing request, all we need to do is give that to the issuing authority so that they can give us a signed public key

UPDATE: Done with the problem of already present in a current certificate after 4 days of talking to godaddy

Now, i can generate my new certificate, but i waiting for 4 days that i could have done without and got it on the first day, the 72 hours written in the manual is probably the MAXIMUM after revoking a certificate, not after canceling it.

Problem, apache will not start without pass phrase, this also means that rebooting the machine will have the machine hang waiting for apache to start and waiting for a user to enter a password for apache, so we need to decrypt the private key
Please note that this does not make your connection less secure, but in the event that someone gets hold of the key file (That you should protect encrypted or not), they can defeat SSL security.

root@someserver:~#/etc/init.d/apache2 restart
Restarting web server: apache2 ... waiting Apache/2.2.16 mod_ssl/2.2.16 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server www.example.com:443 (RSA)
Enter pass phrase:

OK: Pass Phrase Dialog successful.

Anyway, now we should come back to how to remove the pass phrase from the private key

Assuming that your RSA key is stored in the file
/etc/apache2/sslkeys/server.key
To decrypt the file, so that apache does not requer a password with every restart
1- copy the key file:

cp /etc/apache2/sslkeys/server.key /etc/apache2/sslkeys/server.enc.key

Now, decrypt the key (read from the backup file) into the key file in our config

openssl rsa -in /etc/apache2/sslkeys/server.enc.key -out /etc/apache2/sslkeys/server.key

Now the encrypted key is in the server.enc.key just in case you need it, and the key used by apache is NOT encrypted and is in server.key file (That apache already uses)

PHP execution speed et al

There are many tools that precompile PHP to make it run faster, up to now, my favorite is APC which also serves as a very fast value cache (for persistence between requests), a value cache much faster than memcached (but not as distributed).

For some time, i have been optimizing further by asking APC to never check if the file is modified on the disk, and whenever my software is modified i would manually clear the APC cache so that the whole script can be compiled all over again (I say compiled loosely speaking, in reality, it is simply turned into bytecode).

In any case, when you have a server with plenty of ram, it would be convenient if the PHP engine can read the file itself in byte and skip that step for compiled files, and from the way the linux kernel works, those files would be cached from disk into ram (because when a file is read or written, linux keeps a cache of it in ram).

So, bcompiler should be a good extension to PHP that fits such a criteria, and is probably my new way of running my scripts.

Also, bcompiler hides my source code, but i am not interested in that to protect my intellectual property, usually i am not very concerned about my intellectual property because it takes a very good programmer to understand a program and take things from it, and if the person is such a programmer, well, he can also write his own, and with the help of google, he can arrive at something like what i am doing, so to make a long story short, i am interested in hiding my source code for application security reasons, or Security through obscurity as MircoSfot would put it

Checking if SSD trim is working (discard)

Note that if your kernel is before 2.6.33 you can check, but it won’t be working !

in the case that you don’t want to update your kernel, and you just want to trim your disk, try wiper.sh or fstrim, both are command line tools that you can run manually or put in a cron job. if you do want to update your kernel, here is how on debian squeeze

So for example, if you are on debian 6 squeeze, you need to get a kernel from the backports (add the line “deb http://backports.debian.org/debian-backports squeeze-backports main” to your /etc/apt/sources.list then apt-get update then apt-get -t squeeze-backports install linux-image-3.2.0-0.bpo.2-amd64) to get the new kernel, it will then work.

I assume you already have an ext4 file system with discard option in fstab as described on this website

Also note: Many modern SSDs will not reclaim the TRIMmed space., so if using the test below you see zeros, discard (trim) is working 100%, if you don’t. it may or may not be working… but if you wait fdor a significant amount of time, then reboot, the zeros should appear in that exact location even if the disk does not reclaim instantly … happyt trimming, now to the procedure

now, write a file to the ssd (random numbers)
dd if=/dev/urandom of=/hds/ssd300/myfile.bin bs=1M count=3

Find the location where the file begins
hdparm –fibmap /hds/ssd300/myfile.bin

Now, take note of the start address and use it in this command replacing xxxxx

hdparm –read-sector xxxxxx /dev/sdb
You should see random numbers

Delete the file
rm /hds/ssd300/myfile.bin
Sync with the command
sync

Wait for 2 minutes
the issue the same command to read again
hdparm –read-sector xxxxxx /dev/sdb
You should now see all zeros, if you do not, the disk has not been trimmed 🙂

SSD trim command on linux

I am writing this because the stuff you need is not in one place elsewhere, this is what you really need to know, and i want to keep this very short, if you like you can read more elsewhere, this one will only share what you really should know.

1- Do i need trim ?
For reading NO, so if you write once and read 102112913 times, you are good without trimming anything, without trimming, disk writes are slow, reading is absolutely not affected by trim.

2- What is the difference between the ext4 discard option and running fstrim myself manually every once in a while, or even put it in a cron job ?
on ext4 with trim enabled, blocks are trimmed (erased) whenever they are no longer in use by the file system, meaning, when data is deleted from a block, the physical flash memory is erased right after the data deletion, so your disk will remain trimmed all the time, the overhead is not much because the OS knows the block it just freed, so it simply does no more math other than issue a second command to trim, when you run fstrim, fstrim will read the whole file system, and whenever it finds an empty spot, it will trim (hardware erase).

3- i forgot to enable discard, do i just enable it and all is good, is that safe.
Yes it is safe, but enable it, then manually run fstrim only once, or you can wait, and eventually all spots will be trimmed after the get written to and erased again.

Ext4 (the new linux file system) supports TRIM when you mount the disk with the discard option, you can use tools to trim with ext2 or ext3, but it won’t be automatic and not as efficient.

1- But i want ext2 because i don’t want Journaling
ext2 is in fact ext3 without the journal, in ext4 you can remove the journal as well with no problems at all, there are no consequences, ext4 was designed to run with or without journaling

How do i format the disk in ext 4 and enable trim ?
For instructions on creating ext4 partitions, see here , as for the mounting, the line should have an extra option called discard and it should look something like this in your /etc/fstab

UUID=b7a491b1-a690-468f-882f-fbb4ac0a3b53       /hds/3tb            ext4     defaults,noatime,discard                0       1

You should be done, there is nothing more to do

3- I am stuck with ext2 and i don’t want to move, reformet and then move back again
before that, do you know that you can convert the drive to ext4 ?

4- I don’t want to convert anything to anything, i just want to manually trim
Thats easy, use the command
fstrim /hds/myssddisk

and you are done, but mind you, on anything but ext4, this will trim the whole unused space trimed or not trimmed

athurx.sys causes blue screen of death (BSOD) [SOLVED]

athurx is the atheros wireless driver, i have 2 atheros adapters on the same computer, one is 300Mb/s and the other is 150Mb/s

The reason for the blue screen is that the wireless interface driver(S) on my 64bit windows 7 (could be on any other platform too) is outdated, and it seems the old version of the driver has a problem in using 2 different atheros adapters on the same computer, the solution seems to lie in updating the drivers.

1- The Wireless N 150Mb/s (TP-LINK TL-WN722N, atheros AR9271 chip, should also apply to TL-WN721N that uses the same chip)
So, my atheros based TP-LINK TL-WN722N uses the Qualcomm atheros AR9271 chip, Windows installs driver version 2.0.0.32 dated 1/4/2010, and since Qualcomm atheros does not provide their drivers directly on there website, i downloaded the driver from TP-LINK, the new driver had version 2.0.0.62 and was dated more than a year later 4/20/2011, the problem with this driver is that it is NOT SIGNED, and therefore you have to manually install the driver then agree to install a non signed driver, simply asking windows to update from a directory will not update your driver, also check the driver versions before you update to see if your drivers have been updated.

2- The Wireless N 300Mb/s (tp-link TL-WN821N, Atheros AR7015)
This one also has an outdated driver that can be updated from the TP-LINK website, just like the one above.

Make sure you unplug the other driver as you update the software for the first, and once both have drivers installed, reboot and plug in both adapters, works like a dream up to now.

Setting up file sharing on debian lenny / squeeze / wheezy / Buster and Bullseye

This tutorial was first written for Lenny, then tested on squeeze, wheezy… up to bullseye

In August 2021, I have revised this for Debian Buster (10) and Debian Bullseye (11).

If you have installed Debian Lenny with file sharing (in the lenny installer), you will need to add users to that installation and specify folders that need to be shared, here i will show you how to add a user and share a folder with that user.

If you have not installed file sharing while installing Debian lenny, you need to install them, the easiest way to do that is

apt-get install samba

Or if you like

run the command

aptitude

using the arrow keys and the enter key, expand “TASKS” then place the bar over File Server, then the plus sign to select it (Shift +), right after hit “g” then “g” on your keyboard to install them.

Once done, add the following lines to smb.conf

[public]
   comment = Public Folder
   path = /public
   writable = yes
   guest ok = yes
   guest only = yes
   force create mode = 775
   force directory mode = 775
[private]
   comment = Private Folder
   path = /private
   writable = yes
   guest ok = no
   valid users = @smbshare
   force create mode = 770
   force directory mode = 770
   inherit permissions = yes

Now to adding users, from the following 3 commands, After the second and after the third command, you will be asked to chose a password for the user joe

useradd yazeed -m -G users
passwd yazeed
smbpasswd -a yazeed

Now with this out of the way, you can also edit the following fields in the file /etc/samba/smb.conf

The following need to be edited

netbios name : Should become the name of the computer on the network, in windows, this is the machine’s name
hosts allow : If your network is like mine where PCs take IP addresses of the form 192.168.2.x, then this one should be 127. 192.168.2. where 127 is for the local machine, the following part of the IP is the part of the IP that prefixes the IP of all network machines that should be able to access this file server (usually you will change the 2 with a 0 or 1)
interfaces : 127.0.0.1/8 192.168.2.0/24, like above, if your IP subnet is not 192.168.2.x, change it here (usualy you will change the 2 with a 0 or 1)
remote announce : if your subnet does not start with 192.168.2. then change it to your own (usually you will change the 2 with a 0 or 1)
remote browse sync : if your subnet does not start with 192.168.2. then change it to your own (usualy you will change the 2 with a 0 or 1)

Now scroll down, you will see a sample folder, edit that to the folder you want to share, and copy it over and over again for any other folders that you want to share, You are done, now you should be able to open those folders from the network.