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

Use X++ wildcard (LIKE and NOT LIKE) in X++ select statement

For x++ select statements:  select firstOnly batchHistory      where batchHistory.Caption  LIKE  "*Test*"  For x++ queries:  queryBuildRange.value(*Test*); Note the LIKE instead of a '==' and the wildcards inside of the quotations. All other combinations of wildcard usage will work here. This is the same functionality as what the users would put in the grid filtering(eg. '*TEST*' in the caption field filter on the batch tasks form).  However, if you want to find all Captions that do not have the word Test in them (NOT LIKE, !LIKE), you will have to modify the above example slightly.  For x++ select statements:  select firstOnly batchHistory      where  !( batchHistory.Caption LIKE "*TEST*" ) ;  For x++ queries:  queryBuildRange.value(!*Test*);

Cleanup Jobs AX 2012

Cleanup jobs in Dynamics AX 2012 can help delete old data that is not needed anymore, and this reduction in database size can improve performance. In Dynamics AX 2012, cleanup jobs are available across various modules to run manually or in a batch mode. It is important to note that these jobs should be run after enough analysis determines that the business doesn’t need this data in the future. Please be sure to discuss with business management before deleting old data used in transactions that is not required any longer. Dynamics AX 2012 adds data to various tables for staging/intermediate purpose and this staging/intermediate data can be deleted once the transaction has been posted such as Sales update history, Purchase update history data. Here is the list of cleanup jobs available in Dynamics AX 2012 along with brief descriptions. All of these cleanup jobs are available in module > Periodic > Cleanup section Module Job Description General Ledger Clean up...

AX X++ str2Date

From time to time, a user will need to pull a date from a string. More often than not it is from an XML or from some other location. In order to actually use this date passed, it needs to be of data type 'date' so it can be written to fields, used in queries etc.  To do this, a user can use the 'str2Date' function. The str2Date function takes two parameters: datestring = The date in string format. It can be separated by either a dash ('-') or slash ('/') sequence = A three number combo using 1, 2, and 3 where 1 = day, 2 = month, and 3 = year. (e.g. 231 would be MM-YYY-DD, and 321 would be YYYY-MM-YY) If the sequence doesn't match the string or has an error, a '0' will result. Note that its very important to distinguish between day and month as an invalid month (>12) will result in a 0 and if both are <13, the date can translate totally wrong. I've detailed out the results of various scenarios below. Should be pretty self explan...