Tag Archives: phpmailer

PHPMailer: AddReplyTo not working/being ignored Tips, Tricks and Tutorials 15 JUL 2015

PHPMailer is a popular full-featured email creation and transfer class for PHP, and one that I most often use in my projects.

We recently spotted an issue whereby we were setting the AddReplyTo field to a mail object for send out, but on subsequent send, the mail clients seemed to all be ignoring what we had set as the e-mail address which “Reply To” should use! Essentially it was as if AddReplyTo is either not working or being ignored – more than just a little annoying indeed!

It turns out however that there was (or perhaps still is) an issue whereby if an e-mail address is set via the SetFrom call before you set the AddReplyTo call, the Reply To address is simply ignored when the e-mail headers are constructed. So a simple fix is to call AddReplyTo first, before calling SetFrom.

So in other words, the correct order should be:

$mail->AddReplyTo('replyto@email.com', 'Reply to name');
$mail->AddAddress('user@email.com', 'User name);
$mail->SetFrom('mailbox@email.com', 'Mailbox name');

This behaviour may have subsequently changed in newer versions of the PHPMailer class, but if you’re having the same issue that I was having, this is most definitely a quick and easy fix!

postman pat with boombox

Related Link: https://github.com/Synchro/PHPMailer

How to Locate which Ports You can Send out on via SMTP when using a GoDaddy E-mail Account Software & Sites 30 APR 2012

A port is just a port, and just because there is a standard one that is used by something, it doesn’t mean you always have to use the default port for doing that something. Enter sending out an e-mail message via SMTP when using a GoDaddy e-mail account.

Although GoDaddy restricts incoming e-mail to ports 110 and 995 (SSL), outgoing SMTP ports (usually via its smtpout.secureserver.net server) are a little more open. This tutorial will teach you how to find out which ports are available for you to use in your e-mail sending application.

First, login to GoDaddy’s site (http://www.godaddy.com) and click on the “Email” button on the main green menu bar. If you have any active e-mail accounts, they will be listed, and to the right of each entry you will find a green launch button. Clicking on this will open the Control Center.

Once in the Control Center, you’ll see your e-mail addresses listed under the Email Plans folder. The addresses are laid out in a table, with the columns, Address, Size, Relays, Attributes, and Actions.

In the Actions column you will see a Tools icon. Click on it to launch the Info Center.

The Email Info Center is a one-page view of your e-mail address account settings. Under the fieldset entitled “Email Server Settings”, you will spot a label “Outgoing server (SMTP):”, giving you the server details to use, as well as a list of available and open ports. Score!

So if you want to move away from the standard port 25 and perhaps now send out on port 80 via your PHPMailer-using PHP script, your code would look like this:

$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtpout.secureserver.net"; // specify main and backup server
$mail->Port = 80; // new port
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@mydomain.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password

Nifty.

How to Add a CC E-mail Address with PHPMailer Programming 02 APR 2012

I’ve moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class.

Today’s code snippet will show you just how easy it is to add CC (“carbon copy” or “courtesy copy” depending in what era you were born!) e-mail addresses to a e-mail send out, thus saving computing time by pushing out a single mail send instead of a number of separate ones for each attached address (well technically, we’re just foisting all the work onto the SMTP server, but still, less work that the web server needs to do, i.e. a win for us!).

In code:

//create the mail class and fill in all the required settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.net";
$mail->SMTPAuth = true;
$mail->Username = "username@domain.com";
$mail->Password = "password1";
$mail->From = "username@domain.com";
$mail->FromName = "Software Simian";
$mail->AddAddress("targetguy@domain.com", "Target Guy");
$mail->AddReplyTo("username@domain.com", "Software Simian");
$mail->Subject = "Subject";
$mail->Body = "Message";
$mail->AltBody = strip_tags("Message:);
 
//and now for the actual bit of adding multiple CC e-mail addresses
$mail->AddCC("extra-address1@domain.com");
$mail->AddCC("extra-address2@domain.com");
$mail->AddCC("extra-address3@domain.com");
 
//and send. Now was that not easy?
if(!$mail->Send()){
    $resultstatus = 'Failed';
}

The code above will result in a mail being sent out to four addresses instead of just the main specified address.

Nifty.

PHP and PHPMailer: How to Check if Your Mail Send Failed CodeUnit 21 DEC 2011

PHPMailer is a great little mail sending workhorse class for PHP, widely expanding on the regular PHP mail() function and particularly easy to make use of, making it a personal favourite of mine.

Anyway, to check whether or not your mail send was successful when using PHPMailer, all you need to do is check the Boolean variable returned by the Send() function, and if false, check what the error message contained in ErrorInfo is.

In practice:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

if(!$mail->Send())
{
   echo "Error sending: " . $mail->ErrorInfo;
}
else
{
   echo "E-mail sent";
}

As simple as that! :)

PHP: How to Send a PDF File Attachment via E-mail CodeUnit 08 JUN 2011

I’ve moved to the fantastic PHPMailer PHP class to handle all my e-mail send outs for my projects and have thus far been quite impressed by the ease-of-use and robustness of the class. Today I’ll quickly note how you can send an attachment (like a PDF file for example) with a mail send out.

As you can see from the code snippet below, attaching a file is as simple as passing a valid file path to the file to be included to the AddAttachment() function call and then hitting Send().

//create the mail class and fill in all the required settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.net";
$mail->SMTPAuth = true;
$mail->Username = "username@domain.com";
$mail->Password = "password1";
$mail->From = "username@domain.com";
$mail->FromName = "Software Simian";
$mail->AddAddress("targetguy@domain.com", "Target Guy");
$mail->AddReplyTo("username@domain.com", "Software Simian");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body    = "Message";
$mail->AltBody = strip_tags("Message:);

//and now for the actual bit when it comes to adding a file attachment to an e-mail
$mail->AddAttachment("/home/username/fileToUpload/report.pdf");

//and send. Now was that not easy?
if(!$mail->Send()){
    $resultstatus = 'Failed';
}

Couldn’t be any simpler, could it? :)

Related Link: http://phpmailer.worxware.com/