PHPMailer is a PHP class that provides a package of functions to send email . The two primary features are sending HTML Email and e-mails with attachments . PHPMailer supports nearly all possiblities to send email : mail() ,Sendmail , qmail & direct to SMTP server (ie sending email via your Google mail-account ) . You can use any feature of SMTP-based e-mail , multiple recepients via : to , CC , BCC , etc. In short: PHPMailer is an efficient way to send e-mail within PHP .
As you may know, it is simple to send mails with the PHP mail() function . So why use PHPMailer ? Isn’t it slower ? Yes that’s true , but PHPMailer makes it easy to send e-mail , makes it possible to attach files , send HTML e-mail , etc . With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function on *nix platforms .
The following code snippet demonstrates how to implement the class into your script or website and how to build an e-mail application . If you just want to send simple e-mail and you have no problems with the mail() function , it’s suggested that you continue to use mail() , instead of the phpmailer class .
Before continuing , please be sure that PHPMailer class is installed correctly . Just download the phpmailer class from code.google.com , unzip the file and upload only the mailer class ( class.phpmailer.php) to your server . You can upload the class in the same directory as the HTML webpage that use mail functionality , or alternatively , upload the class into your lib folder (my lib is outside the publicly available directory ) . If you’re still not sure, you can verify that you’ve installed PHPMailer correctly with this little script :
1
| <?php require("class.phpmailer.php"); $mail = new PHPMailer(); ?> |
Save it as a PHP document in the same directory where you’ve savedclass.phpmailer.php . If no errors result from running the script , your installation has been done correctly . If you switched error messages and warnings off , then go ahead and set it on ; refer to your PHP manual for more on this topic .
If the previous step was completed , try to send a message with an attachement :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| <?php require_once 'class.phpmailer.php';$mail = new PHPMailer();// Now you only need to add the necessary stuff// HTML body$body = "</pre><div>";$body .= " Hello Dimitrios";$body .= "<i>Your</i> personal photograph to this message.";$body .= "Sincerely,";$body .= "phpmailer test message ";$body .= "</div>" ;// And the absolute required configurations for sending HTML with attachement$mail->AddAddress("sendemailto@mail.zz", "My-webpage Website");$mail->Subject = "test for phpmailer-3";$mail->MsgHTML($body);$mail->AddAttachment("phpmailer.gif");if(!$mail->Send()) {echo "There was an error sending the message";exit;}echo "Message was sent successfully";?> |
PHPmailer without attachment :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php require_once 'class.phpmailer.php'; $mail = new PHPMailer(true); $to = "sendmailto@gmail.com"; $subject = "this is a test from phpmailer" ; $message = "This message was send with the PHP-mailer library and uses the defauld (mail) "; try { $mail--->AddAddress($to, 'Example To');$mail->SetFrom('testexample@example.com', 'Example');$mail->AddReplyTo('example@example.com', 'Example');$mail->Subject = $subject;$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically$mail->MsgHTML($message);$mail->Send();echo "Message Sent OK\n";} catch (phpmailerException $e) {echo $e->errorMessage(); //Pretty error messages from PHPMailer} catch (Exception $e) {echo $e->getMessage(); //Boring error messages from anything else!}?> |
Komentar
Posting Komentar