Langsung ke konten utama

A Simple Example : Sending Email with Attachment Using Phpmailer

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

Postingan populer dari blog ini

Format Label Tom Jerry (MS Word)

Setelah beberapa lama aku tidak membuat label undangan, tiba-tiba kemarin aku diminta untuk membuat label undangan Wisuda dengan menggunakan label merek Tom & Jerry (TJ). Harusnya sih menjadi tugas sekretaris panitia tapi karena yang bersangkutan ada tugas di luar kantor maka aku yang membereskannya. Mula-mula aku membuat format labelnya dengan cara manual, tapi untung ada pak Mulyadi (staf tata usaha) yang menyarankan aku untuk mengunduh format label yang telah ada di Internet. Mulailah aku browsing di google dan menemukan format label berbagai ukuran dari website Tom & Jerry di alamat berikut  http://tjlabels.com/en/download.html  (yang link ini agak rempong krn pake masukin nomor captcha-captcha-an). Sedangkan kalau yang link ini langsung pilih file ga pake nomor captcha-captcha-an ( http://tjlabels.com/assets/download/ ). Ternyata juga ada banyak berbagai blog yang menyediakan  link download seperti ini, tapi aku mem- posting  artikel ini adalah untuk diri sendiri, ya

Solved : Update on a valid time state table is not allowed without specifying a ValidTimeStateUpdateMode

How To Solve Error :  Cannot edit a record in Position hierarchies (xyz). Update on a valid time state table is not allowed without specifying a ValidTimeStateUpdateMode. select forUpdate * from xyz where xyz.recid== _RecId ; try { ttsBegin; _Value= “Abcd”; xyz.Name = _Value; xyz.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction); xyz.ValidFrom =today(); xyz.ValidTo=dateMax(); xyz.update(); ttsCommit; } catch { ttsAbort; }

Cara membuat Harddisk External menjadi Bootable USB untuk install Windows

Untuk install windows dari harddisk external, silahkan siapkan hal hal berikut ini gan : Harddisk External File Image (ISO) dari Windows yang mau digunakan Laptop/ Komputer   Oke, kita masuk ke  LANGKAH PERTAMA :   Setelah kita persiapkan hal hal yang ada diatas, ada baiknya kalo antivirus / Windows Defender nya itu matiin dulu (recommended) bagi harddisk nya terdapat banyak crack. Karena crack itu bersifat virus dan pasti bakalan langsung keapus sama antivirusnya. Pastiin kalo harddisk ente itu ada space minimal 5 – 10 Gb untuk penempatan File Image Windows yang akan dipakai. Recommended 10Gb Abis harddisk nya di colokkk silahkan buka  Control Panel  >  Administrative Tools    Lalu buka  Computer Management Kalo udah kebuka, pilih menu  Disk Management  yang ada di toolbar kiri. Contoh dibawah ini   Karena ane make harddisk yang merknya Toshiba, maka yang akan kedetek itu  Wibi Toshiba  pada bagian  Disk 1 Langung  Kli