How to Use PHP Mailer
Receiving bulk emails can be an annoyance, but they are very important to businesses and non-profit organizations seeking to solicit customers and support. In addition, bulk emailing helps keep the target audience informed about new products and services. PHPMail is an open-source PHP library that eases the process of sending HTML emails and attachments. PHPMail works best on Web servers with PHP5 capability, but there are PHP 4 versions as well. The PHPMail archive contains documentation for PHPMail and the PHP language as it applies to email. There are also example files that help you get started and sending email quickly.
Things You'll Need
- Plain text editor
- Access to a Web server with PHP5 Available
- FTP File Transfer Protocol application
- Latest version of PHPMail from the developer's website
- Active SMTP email account and email address
Instructions
-
-
1
Launch the standard, plain-text text editor application that is available on your computer.
-
2
Enter the following code into the text editor. Change "smtp.yourmailserver.com" to your SMTP mail server address. Change "your@smtpemailaddress.com" to your return email address. Change "personreceiving@hismail.com" and "John Doe" to the email address and name of the person to whom you are sending the email. This code is a modification of an example file included with PHPMail:
<?php
include_once('class.phpmailer.php');
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.yourmailserver.com"; // SMTP server
$mail->From = "your@smtpemailaddress.com";
$mail->FromName = "FromYou";
$mail->Subject = "PHPMailer Test Subject via smtp";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("personreceiving@hismail.com", "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
-
-
3
Download the latest version of PHPMail from the link at the developer's website, http://phpmailer.worxware.com. Other sites may have this file, but the developer will have the most recent releases. Extract the file on your computer using your system's file archive extraction utility.
-
4
Click the File menu. Select the "Save" option. Save under the file name "test.php."
-
5
Click the File menu. Select the "New" option. Enter the following code into the text editor:
<html>
<head>
<title>Your Title</title>
</head>
<body>
<p>This is to introduce me to your by email</p>
</body>
</html>
-
6
Click the File menu. Select the "Save" option. Save under the file name "content.html."
-
7
Click on the File menu and select "Quit" or "Exit" to close the text editor.
-
8
Launch the FTP application and log in to the Web server.
-
9
Upload the files "test.php" and "content.html" to the root directory of the web server. Use your FTP application to go to the PHPMail directory that your file archive extraction utility created and upload the files "class.phpmailer.php" and "class.smtp.php" to the root directory of the Web server.
-
10
Click on the "Disconnect" button of the FTP application.
-
11
Launch a Web browser and enter the appropriate URL to access the PHP file. Example enter:
http://yourdomainname.com/test.php
Replace "yourdomainname.com" with the domain name or IP address of the Web server.
-
12
Press the enter key to load the URL and run the PHP file. If the mailing was successful, the browser should display "Message sent!"
-
1
Tips & Warnings
PHP has a built-in function "mail()" that uses the Web host's "sendmail" utility to send email. The format for sending a simple email is:
mail(receiver_email_address, subject, message);
The mail() function can be part of a script that uses a contact list and tailors the message to each user. You can be as simple or as sophisticated as your scripting skills allow.
PHPMail may not work with all shared hosting Web servers. Sometimes, a perfectly crafted PHPMail script may return the error: "phpmailer Mailer Error: SMTP Error: The following SMTP Error: Data not accepted." Correcting this requires some changes to the "class.phpmailer.php" file that are beyond the scope of this article. Check with your Web host about other bulk email alternatives.
References
Resources
- Photo Credit Photodisc/Photodisc/Getty Images