Sending Emails using PHP: Mail Function and Attachments, Lecture notes of Computer Science

A step-by-step guide on how to send emails using PHP's mail function, including setting up the mail server, formatting the email message, and sending emails with attachments. It also covers sending emails to multiple recipients with CC and BCC addresses.

Typology: Lecture notes

2019/2020

Uploaded on 01/24/2020

kris-covers
kris-covers 🇮🇳

6 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP- Mail
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Sending Emails using PHP: Mail Function and Attachments and more Lecture notes Computer Science in PDF only on Docsity!

PHP- Mail

If you are in a *NIX environment, you will most likely have

sendmail installed on your server. If you are using a hosting service,

check with your service provider to make sure sendmail or some

equivalent is being used. Once you have your mail server squared

away, you’ll need to modify your php.ini

1.SMTP - Set this to the ip address or DNS name of your SMTP

server. default value : localhost

2.sendmail_from - The from address used by default by the PHP

mail() command

3.smtp_port - Set this to the port PHP uses to connect to the SMTP

server. default value 25

Email

Output: The mail was sent to [email protected] successfully.

Simple Mail Program

Can collect the data through HTML forms and can send an email. Should create one HTML program to collect information like to-address, subject, message from user; and one PHP program to send mail using these information.

Sending Email using Forms

[email protected] [email protected] Hello World…. …. We will be there soon!

Output: The mail was sent to [email protected], [email protected], [email protected] successfully.

Email to Many addresses

The cc & bcc addresses to be included in header parameter as

follows:

$header=“cc: [email protected],[email protected]

Or

$header=“bcc: [email protected],[email protected]

If u want to add both together, then

$header =“cc: [email protected],[email protected] \r\n ”

$header. =“bcc: [email protected],[email protected]

Here \r\n – is the boundary between cc & bcc. It is must to use \r\n

to the header, if you want to add more parameters.

Email with CC & BCC

Output: The mail was sent to [email protected] cc:[email protected], [email protected] bcc: [email protected] successfully.

In order to send messages as a plain text and an HTML, will use headers with

additional information as follows:

$headers = “MIME Version 1.0 \r\n”;

$headers .= "Content-type: text/html; charset=iso- 8859 - 1 \r\n";

$headers .= "Content-Transfer-Encoding: 7bit\r\n";

Tell the e-mail client that data is coming in multiple parts—in this instance, plain text and HTML. This tells the e-mail client to look for additional “Content-type” information in the message, which includes boundary information. The boundary is what separates the multiple parts of the message. Email – MultiPart messages

To send a email with an attachment, should do the following steps:

Step 1 : Open the attach file and transfer its content to a php variable.

Step 2: Assign that file content to the message parameter of the mail

function with some attributes and boundary which are needed for

attachment.

Step 3: Now add the message which has to be delivered to the receiver

with attributes to specify it is a message.

Note: Here, every information has to be separated with a boundary value. Email with an attachment

The above steps will explain in detail with program codes

  1. Open the file which has to be attached with the mail in rb mode and transfer the content into a php variable. $fileatt = "mysql-php.pdf"; // Path to the file $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); Set the attributes for attachment 2.1 Now add the content-type to the message as the type of attached file. $email_message = "--{$mime_boundary}\n". "Content-Type: {$fileatt_type};\n“;

3.1 Now can add message which has to send as a plain or HTML: $email_message .= “your file has been sent as an attachment to the user specified by you”. 3.2. Now add attributes for message. $email_message .= "This is a multi-part message in MIME format.\n\n" ."-- {$mime_boundary}\n". "Content-Type:text/html; charset="iso- 8859 - 1 "\n". "Content-Transfer-Encoding: 7bit\n\n". After done all the above use mail() as follows: $headers = "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary="{$mime_boundary}""; $sent=mail($to, $sub, $email_message,$headers);

The following slides show the sample email program with an attachment php /* IMAGE ATTACHMENT $fileatt = "test1.jpg"; // Path to the file $fileatt_type = "image/jpg"; // File Type $fileatt_name = "test1.jpg"; // Filename that will be used for the file as the attachment */ // TEXT ATTACHMENT $fileatt = "MYSQL-PHP.pdf"; // Path to the file $fileatt_type = "application/pdf"; // File Type $fileatt_name = "PHPebook.pdf"; // Filename that will be used for the file as the attachment

Sample program