PHP mail sending PHPMailer usage example

Source: Internet
Author: User

PHP mail sending PHPMailer usage example

The example in this article describes PHPMailer usage of PHP mail sending class and detailed the operation steps. Share it with you for your reference. The procedure is as follows:

1. Install sendmail on the server

1

Sudo apt-get install sendmail

2. Start sendmail

1

Sudo/etc/init. d/sendmail start

3. Modify php. ini

1

2

3

4

[Mail function]

SMTP = localhost

Smtp_port = 25

Sendmail_from = me@example.com

4. Function sendMail Function:

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

<? Php

/* Call PHPMailer to send an email

* @ Param String $ receiver recipient

* @ Param String $ sender

* @ Param String $ sender_name if the sender name is null, use the sender address instead.

* @ Param String $ subject

* @ Param String $ content

* @ Param boolean $ whether ishtml is an html email

* @ Param Array $ attachements attachment

* @ Return boolean

*/

Function sendMail ($ receiver, $ sender, $ sender_name, $ subject, $ content, $ ishtml = true, $ attachments = array ()){

Include_once "class-phpmailer.php ";

If (empty ($ receiver) | empty ($ sender) | empty ($ subject) | empty ($ content )){

Return false;

}

$ Mail = new PHPMailer ();

// $ Mail-> IsSMTP (); // sent by smtp

// $ Mail-> Host = "smtp.gmail.com"; // SMTP Server

// $ Mail-> Port = 465; // SMTP Port

// $ Mail-> SMTPSecure = 'ssl '; // Encryption Method

// $ Mail-> SMTPAuth = true; // enable SMTP authentication

// $ Mail-> Username = "username"; // User Name

// $ Mail-> Password = "password"; // Password

$ Mail-> IsMail (); // The using PHP mail () function may prompt that this email may not be sent by the following users.

$ Mail-> From = $ sender; // sender

$ Mail-> FromName = $ sender_name; // sender alias

$ Mail-> AddReplyTo ($ sender); // reply

$ Mail-> AddAddress ($ email ER); // recipient

// Send in html Format

If ($ ishtml ){

$ Mail-> IsHTML (true );

}

// Send the attachment

If ($ attachments ){

If (is_array ($ attachments )){

$ Send_attachments = array ();

$ Tmp_attachments = array_slice ($ attachments, 0, 1 );

If (! Is_array (array_pop ($ tmp_attachments ))){

If (isset ($ attachments ['path']) {

Array_push ($ send_attachments, $ attachments );

} Else {

Foreach ($ attachments as $ attachment ){

Array_push ($ send_attachments, array ('path' => $ attachment ));

}

}

} Else {

$ Send_attachments = $ attachments;

}

Foreach ($ send_attachments as $ attachment ){

$ Attachment ['name'] = isset ($ attachment ['name'])? $ Attachment ['name']: null;

$ Attachment ['encoding'] = isset ($ attachment ['encoding'])? $ Attachment ['encoding']: 'base64 ';

$ Attachment ['type'] = isset ($ attachment ['type'])? $ Attachment ['type']: 'application/octet-stream ';

If (isset ($ attachment ['path']) & file_exists ($ attachment ['path']) {

$ Mail-> AddAttachment ($ attachment ['path'], $ attachment ['name'], $ attachment ['encoding'], $ attachment ['type']);

}

}

} Elseif (is_string ($ attachments )){

If (file_exists ($ attachments )){

$ Mail-> AddAttachment ($ attachments );

}

}

}

$ Mail-> Subject = $ subject; // mail title

$ Mail-> Body = $ content; // email content

Return $ mail-> Send ();

}

// DEMO example:

$ Author ER = 'author er @ test.com ';

$ Sender = 'sender @ test.com ';

$ Sender_name = 'sender name ';

$ Subject = 'subjecct ';

$ Content = 'content ';

// All four formats are supported

$ Attachments = 'attachment1.jpg ';

$ Attachments = array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg ');

$ Attachments = array('attachment1.jpg ', 'attachment2.jpg ');

$ Attachments = array (

Array ('path' => 'attachment1.jpg ', 'name' => 'attachment 1.jpg '),

Array ('path' => 'attachment2.jpg ', 'name' => 'attachment 2.jpg '),

Array ('path' => 'attachment3.jpg ', 'name' => 'attachment 3.jpg '),

);

$ Flag = sendMail ($ sender er, $ sender, $ sender_name, $ subject, $ content, true, $ attachments );

Echo $ flag;

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.