How to Send From Another Email Address Using PHP ASP Send Email Script

To sustain this free service, we receive affiliate commissions via some of our links. This doesn’t affect rankings. Our review process.

Email windowLooking to send email from another email address? First of all, why would you want to do this? Legitimate reasons include wanting to unsubscribe from an email newsletter, to which you are subscribed with an alias, or forwarding address. Let us explain. We typically subscribe to email newsletters with forwarding addresses, or aliases, that correspond to that service. For example, if we wanted to subscribe to a CafePress newsletter, we would subscribe with the email address “cafepress@example.com“. This alias would then forward to our real email address, i.e. “abc@example.com.” This way, if there’s ever a spam issue, or we receive unsolicited email, we can trace where it came from.

A problem with this approach arises with email newsletters that don’t use an unsubscribe link, but rather, require you to reply with an unsubscribe email (usually an empty email message with the words “unsubscribe” in the subject line) from the email address that is subscribed to the newsletter. Well, if that email address is an alias, how are you going to send from it? You would need to create an email account for each and every alias you have subscribed to newsletters that require this unsubscription method, which would be a pain, and in some cases, a costly pain, if it costs you to add a mailbox. We’re here to show you an easier method – one you can use simply by uploading a file to your server.

PHP ASP Send Email Script

Here’s a php asp send email script SMTP authentication script that you can copy into a file with the extension .php and upload to your hosting server (ie. into php-smtp-mail.php; this assumes that your website is hosted on a Linux/ Apache server that runs PHP scripts). If you’re on a Windows server, that uses .ASP scripts, scroll down to the ASP send email script. This script includes SMTP authentication – a method used by hosting providers to reduce spam sent from the server. Basically it requires one email account (we usually use noreply@example.com) with a password that the script uses to send the email message. This way, malicious users can’t hijack your email script and send mass emails using your server, which will get you blacklisted in the long run, and you’ll have trouble sending emails anywhere. Whether or not your server enforces SMTP authentication, it’s a good idea to use it to cover your own behind. Without further ado, here’s the script:

><?php
require_once “Mail.php”;

$from = “Sender Name <from@example.com>”;
$to = “Recipient Name <to@example.com>”;
$subject = “Unsubscribe”;
$body = “”;

$host = “mail.example.com”;
$username = “smtp@example.com”;
$password = “password”;

$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo(“

” . $mail->getMessage() . “

“);
} else {
echo(“<p>Message sent successfully!</p>”);
}
?>

Make sure you fill in all the variables in bold. The email address from@example.com should be the email address you want to send from (ie. the one you’re subscribed to the newsletter you wish to unsubscribe from with). Also, you’ll want to replace smtp@example.com and the corresponding password with those belonging to the email account you created for SMTP authentication (ie. noreply@example.com). Next, simply upload your file to your hosting server (ie. example.com/php-smtp-mail.php) and load that URL in your browser. If successful, you’ll get a “message sent successfully!” confirmation, and you should hear from the newsletter provider you unsubscribed from shortly thereafter.

ASP Send Email Script (with SMTP Authentication)

This example uses ASP, for Windows servers, to do the same thing – send an email from an address you specify using SMTP authentication:

<%
using System.Net.Mail

MailMessage msgMail = new MailMessage();

MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress(“from@example.com”);
myMessage.To.Add(“to@example.com”);
myMessage.Subject = “Unsubscribe”;
myMessage.IsBodyHtml = false;

myMessage.Body = “Message Body”;
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(“smtp@example.com”, “password”);
mySmtpClient.Host = “mail.example.com”;
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;

mySmtpClient.Send(myMessage);
myMessage.Dispose();
%>

Again, fill in all the variables in bold, place the code into an ASP file, upload it to your server, and load the file in a browser.

You should now be able to unsubscribe successfully from all those pesky newsletters. We recommend that you upload this to a domain that matches the email domain you’re sending from (ie. if you’re sending an email from example.com, make sure your SMTP authentication address is from example.com, and that you upload and run the email send file from example.com). If you’re having issues, please comment below.

About The Author:

Alex has been involved on the business side of the internet since the early 2000's. He holds both a Management Science degree from the University of California at San Diego as well as a Computer Science degree from NJIT.

We Rock Your Web had its roots back in 2004 as the tech blog for a web design and development company Alex founded that has grown and evolved into the parent company of We Rock Your Web.

While his foundation is rooted in web development, his expertise today lies in content and digital marketing, SEO, organic and paid search, analytics, and publishing. Alex is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs.

Disclaimer: This website contains reviews, opinions and information regarding products and services manufactured or provided by third parties. We are not responsible in any way for such products and services, and nothing contained here should be construed as a guarantee of the functionality, utility, safety or reliability of any product or services reviewed or discussed. Please follow the directions provided by the manufacturer or service provider when using any product or service reviewed or discussed on this website.

New comments are closed at this time.

Comments (5)