Technology & Web Resources For Your Business
Home > Tech > Email > How to Send From Another Email Address Using PHP ASP Send Email Script

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

How do we keep this site running? This post may contain affiliate links — the cost is the same to you, but we get a referral fee. Compensation does not affect rankings. Thanks!

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.

Alex bring a series of in-depth articles on search marketing and content management systems as well as troubleshooting tips to We Rock Your Web's collection. He is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs, Bella and Lily.

Leave a Reply

5 Comments on "How to Send From Another Email Address Using PHP ASP Send Email Script"

avatar
Sort by:   newest | oldest | most voted
bounces
bounces

Hi, what’s the difference between recipient email $to in send() method and in $headers variable?

Anonymous
Anonymous

Thanks for this great tutorial – it’s gotten me up and running with a quick and efficient way to send reply emails from my many microsites.

Question – how do I implement Cc: or Bcc: using the Pear/ PHP send() method above? It looks like it’s different from the standard PHP mail() method.

Thanks!

We Rock Your Web
We Rock Your Web

Great question. The method does indeed differ with the send() protocol. The difference is that when you’re using smtp authentication, you need to list the cc/ bcc variables both as a recipient (which decides where the e-mail is sent) and in the header (which tells the mail client how to display the cc/bcc). You can use an array to gather the email address, or simply implement as follows:

$to = “to@example.com”;
$bcc = “User 1 , User 2 “;
$recipients = $to.”,”.$bcc;

$headers[‘From’] = ‘From Name ’;
$headers[‘To’] = $to;
$headers[‘Bcc’] = $bcc;
$headers[‘Subject’] = ‘Re: Reply to Your Question’;

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

P

Someone has hacked my email, is there a way to trace where an email came from?

Anonymous
Anonymous
I liked that the article started out listing why you would want to send an email from another address. However, the one reason chosen by the author to be legitimate does not even remotely apply to me. Plus, the author seems to have made up this one reason and run with it for the whole article, even though there were some holes in the logic and I could not foresee myself actually going through this much of a hassle just to subscribe to a newsletter. The first part that struck me as odd was when the author says you can… Read more »
wpDiscuz

Send this to a friend