We Rock Your Web in the Press!
Home > Tech > Apps and Software > 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

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 “[email protected]“. This alias would then forward to our real email address, i.e. “[email protected].” 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 [email protected]) 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 <[email protected]>”;
$to = “Recipient Name <[email protected]>”;
$subject = “Unsubscribe”;
$body = “”;

$host = “mail.example.com”;
$username = “[email protected]”;
$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 f[email protected] 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 [email protected] and the corresponding password with those belonging to the email account you created for SMTP authentication (ie. [email protected]). 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(“[email protected]”);
myMessage.To.Add(“[email protected]”);
myMessage.Subject = “Unsubscribe”;
myMessage.IsBodyHtml = false;

myMessage.Body = “Message Body”;
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(“[email protected]”, “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.

Our site's mission is to help consumers make more informed purchase decisions. This website accepts financial compensation from some of the companies mentioned which allows us to provide this free service to our readers. Compensation does not influence the rankings of products. More info on our disclosure page.

Sign up for 9 Must Have Social Media Tips and Free Monthly Tech Tips & Reviews!


About Alex Schenker
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.
Previous:
Next:
  • 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

      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 = “[email protected]”;
      $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);

  • 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 subscribe to a newsletter with an alias email and then have that email address forward to you. How do you put in an alternate, or ‘alias’ email address that apparently has no email account attached to it and start having emails addressed to the alias come to your own inbox instead? I am very confused about that.

    Reading how to generate code for PHP that includes SMTP authentication, however, is quite fascinating. Clearly the author would not even attempt to show you how to do this unless he or she was sure that the code was correct, which definitely was impressive. I got to thinking that despite the holes in the original explanation, that copying this for my own Horde email was a much better idea than not doing so.

    However, the language of the instructions even seemed like they were written for more advanced users than I, and I would say I know about as much as the standard person who works on a computer every day and uses my computer extensively at home, as well. So the whole process was losing me because I was not following along as well as not really understanding the purpose of doing this. Furthermore, it seems that if you know enough to be subscribing to a newsletter under an alias and having the newsletter forwarded, you probably also know how to unsubscribe from that newsletter. And frankly, people should choose newsletter subscriptions carefully, since most anyone who has done this in the past knows that it can fill up your email inbox quickly if you are not keeping up with all the reading.

    I would not reference this article again personally, if only because I find the whole process of an alias to be confusing and a lot of extra work for something that I do not see as necessary. Granted, people who are in this situation now might be more apt to disagree with me on that fact, but this is just my opinion. That said, it is great to know that I can go on the internet at any time and find pieces of useful code and instructions on how to use it to correct almost any crisis situation, including this one. So I am not saying that the article has no use, only that it is written for someone who has more computer experience and expertise than I have.