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

When you purchase through links on our site, we may earn a commission. Here’s how it works.

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.

Tagged With:

The information provided through this website should not be used to diagnose or treat a health problem or disease; it is not intended to offer any legal opinion or advice or a substitute for professional safety advice or professional care. Please consult your health care provider, attorney, or product manual for professional advice. Products and services reviewed are provided by third parties; we are not responsible in any way for them, nor do we guarantee their functionality, utility, safety, or reliability. Our content is for educational purposes only.

Subscribe
Notify of
5 Comments
Newest
Oldest Most voted
Inline Feedbacks
View all comments