Sending an email with a different From: address

Sometimes you have to send an email with a different “From:” address. This can be easily done in LINUX. Please see the video below, and read the text below the video for further explanations. If you have questions, please, just add your comment, I will be happy to answer 🙂

Was the video helpful? Here are some explanations of the commands that I used:

The:

export EMAIL="Title Firstname Lastname <differentaddress@validemail.uib.no>"

sets the environment variable EMAIL to what you specify between the two “”. Note, the syntax has to be correct, remember the two <>

The command:

echo -e "some text" | mutt -s "subject text" somename@somewhere

means:

Send the text “some text” through a pipe (the character “|”)  to the program “mutt”, which will send the email to somename@somewhere with the subject “subject text” and the body-text “some text”

If you need to send to multiple people, this is achieved by making the list of email addresses separated with a comma. For example:

echo -e "my message to you" | mutt -s "my subject text" firstperson@something.no,secondperson@somethingelse.no

NB: The Linux command above is written on one-line. No Enter or Returns on keyboard should be done.

Now you can send your emails with any From: address you like.

Remember: it is still your account that sends the email, so changing the From: address doesn’t hide your real identity in the email system.

Akismet does the work!

So far I have had 700 spam posts on my blog. Every one of these spam posts where detected by Akismet, and successfully put to the Spam Folder.

Good work, Akismet. 🙂

Alert Blog Admin php script

I had to send emails to all the Blog admins in our WordPress Multisite installation. This is how I did it:

[code lang=”php”]
<?php
# Alert Blog Admin php script
# connect to blog db, select from, find all blog admin email adresses
# get subject, body from pre-created local files, send the email

# how to run the script
# php alertblogadmin.php -s subjecttext.txt -b bodytext.txt

$mysqlserver="localhost";
$mysqluser="dbuser";
$mysqlpassword="XXXXXXX";
$database="blog";

$ERROR=0;

# This script require arguments
$arguments = getopt("s:b:f:dr");

# Test if array $arguments as at least s,b and r
# maybe to make sure that $arguments array contains at least 3 items?
if ( count($arguments) < 3 ){
echo "Missing arguments…\n";
echo "Usage: php alertblogadmin.php -s subjecttext.txt -b bodytext.txt\n";
die;
}

# 2 text files need to exist
if ( !file_exists($arguments["s"])) {
echo "File $arguments[s] does not exist?\n";
$ERROR=1;
}
if ( !file_exists($arguments["b"])) {
echo "File $arguments[b] does not exist?\n";
$ERROR=1;
}

if ($ERROR == "1"){
die;
}

$link = mysql_connect($mysqlserver, $mysqluser, $mysqlpassword);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db ($database,$link);

# collect blogs
$sql="SELECT blog_id from wp_blogs where deleted not like ‘1’";
$result=mysql_query($sql,$link);
if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}

$i = "0";
$emailarray = array();

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$blog_id = $row[0];
$table = "wp_" . $blog_id . "_options";

# Find email adresses to admins in each blog in the system
$sql2="SELECT option_value FROM $table WHERE option_name = ‘admin_email’";
$result2=mysql_query($sql2,$link);
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
$emailarray[$i] = $row2[0];
}
$i++;
}

# sort the email array and find unique emails
asort($emailarray);
$emailarray = array_unique($emailarray);

$emails = "";
foreach ($emailarray as $email) {
$emails .= $email.",";
}

$emails = substr($emails,0,-1);

# Could be smart to see who you will be sending your email to
echo $emails;

# open subject and body text files
$subject = file_get_contents($arguments["s"]);
$body = file_get_contents($arguments["b"]);
$from = $arguments["r"];

# Use this one to test before you actuelly send to $emails, which is a variabel that might contain hundreds of emails.
# You really would like to test first
$bcc = "user1@something.com,user2@something.net";
//$bcc = $emails; # uncomment this one, when you are ready to go

$headers = "From: noreply@something.com" . "\r\n" .
"Reply-To: noreply@something.com" . "\r\n" .
"Bcc: $bcc" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$header_ = ‘MIME-Version: 1.0’ . "\r\n" . ‘Content-type: text/plain; charset=UTF-8’ . "\r\n";

mail(”, ‘=?UTF-8?B?’.base64_encode($subject).’?=’, $body, $header_ . $headers);
mysql_close($link);
?>

[/code]

Use ‘mutt’ to send email with attachment

The unix ‘mutt’ command can be used to send an email with attachment:

mutt -s "Test message" name@something -a files.tar < message.txt

where files.tar is the attached file, and message.txt is the file containing the message.

Subject to the email is added with the -s option. In this case:
-s “Test message”

name@something is the receiver email address

You can also do this:

echo "Message text" | mutt -s "Subject text" name@something -a files.tar

Reference: http://www.cyberciti.biz/tips/sending-mail-with-attachment.html

Why would you like to use a unix command to send an email with an attachment?
The answer is that in the case you want to automate some process, let us say, you would like to pack together some important files, and send them every Sunday to a certain person. It could be statistical data, for instance, and the person would like to have a report each Sunday so to read them on Monday before lunch.
You could then make a simple bash script that uses ‘tar’ to collect the files into a single tar-file. Then call on the unix ‘mutt’ command, and send the message together with the important tar-file. All can be automated by calling this bash script from cron (unix job scheduler)