Skip to main content

Sending Email in PHP

How to send email using PHP. The following section describes that with coding…..

Before using Mail in PHP, note that, PHP mail is intended for only a small scale use and not for large scale Email systems. So for large scale, you may use other mal softwares.

Email can be sent

  • as ordinary plain message
  • as an HTML Message
  • with attachments

We will see, one by one

Mail in PHP

there is a function mail() which helps in sending mails through PHP Scripts.

the syntax is mail(To address, Subject, Message, headers)

To address – to which address you want to send mail

Subject of the mail -

Message – body of the mail and

headers – are the http headers sent along with the mail to inform the email clients

Example Headers are

  1. “From:abc@xyz.com ” //to inform the from address
  2. “Reply-To:xyz@xyz.com ” //Reply to a different email id
  3. “Bcc:a@a.com ” //Blind Carbon Copy
  4. “Cc:b@b.com” //Carbon Copy
  5. Return-Path:abc@a.com”
  6. X-Mailer: PHP5 //inform the email client
  7. MIME-Version:1.0
  8. Content-Type: text/plain” //text/html or multipart/mixed, multipart/alternative (content type is useful for attaching files with MIME headers)
  9. Content-Disposition: attachment;filename=”image.jpg”
  10. Content-Transfer-Encoding: base64″ //Encoding Standards

Email as ordinary Plain message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$m=mail($to,$subject,$message);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The mail() function returns a boolean value, so if the mail is sent successfully, it returns 1 else 0. Since the above script does not uses any headers, the mail will be sent as plain text mail and the H1 tag wont be validated for a HTML.

The following code send mail as a HTML page

Email as HTML message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$headers = “Content-Type: text/html \r\n”;

$m=mail($to,$subject,$message,$headers);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The above script uses a MIME type as text/html, to the message with H1 rendered as an HTML Tag and the message will display with size of header 1

Email with attachments

Lot of headers to be used for Emails with attachments.

<?php

//set the to, subject and message
$to=”tspembedded@gmail.com”;
$subject=”Here’s the mail with attachment”;
$message=”I am attaching a PDF file for your information”;

//Open the file to attach

$filename=”rts.pdf”; //name of the file and its path

$f=fopen($filename,”rb”); //open the file under binary mode
$data=fread($f,filesize($filename));  //read the file and its size
fclose($f); //close the file pointer
//header setting
$data=chunk_split(base64_encode($data)); //split the file and encode each chunks to base 64
$headers=”Content-Type: application/pdf \r\n”; //since it is a pdf file, mention the mime type
$headers.=”Content-Disposition: attachment;filename=rts.pdf \r\n”; //header to attach the file with filename
$headers.=”Content-Transfer-Encoding: base64 \r\n”.$data.”\r\n”; //header with encode setting

$m=mail($to,$subject,$message,$headers);
if($m)
echo “mail to $to is a success”;
else
echo “Mail to $to Failed”;
?>

the Mime type used in the above script is application/pdf, so only the attachment will be shown in the email client, but the message wont be showed at all…

to make mixed mode, use the mime type as multipart/mixed.

For any other queries and suggestions, Please Comment!!!!!!

Comments

  1. [...] http://pradeepkumar.org/2009/09/sending-email-in-php.html Categorized under: Uncategorized. Tagged with: no tags. [...]

    ReplyDelete
  2. Great work .. but would be excellent if you could add syntax highlighter plugin to show the PHP code in highlighted way :)

    ReplyDelete
  3. Nice tutorial. I was looking for this ...

    ReplyDelete

Post a comment

Popular posts from this blog

Installing TexLive 2019 in Ubuntu 18.04

Installation of TexLive 2019 in Linux (Ubuntu 18.04 LTS)
TeX (Tech)

Installation of TexLive 2019

Please watch the video for full installation



I used .iso file to download, the Total size is 3.3GB for Linux,

and i used the torrent file to download, it took me just 20 min to download the entire .iso file

Extract the .iso file to a folder and open a terminal

$] sudo ./install-tl
(it goes into a terminal mode, which is faster compared to the GUI Mode)

$] sudo ./install-tl -gui
after the installation, set the PATH, MANPATH and INFOPATH as suggested by LATEX

export PATH=$PATH:/usr/local/texlive/2019/bin/x86_64-linux
export MANPATH=/usr/local/texlive/2019/texmf-dist/doc/man
export INFOPATH=/usr/local/texlive/2019/texmf-dist/doc/info

put these lines in to the /home/pradeepkumar/.bashrc

$] gedit /home/pradeepkumar/.bashrc
We have installed TexLive 2019 and texstudio.

To install texstudio

$] sudo apt install texstudio
The look and feel of TexStudio looks like this image.


texlive, it install everyt…

Implementing a new system call in Kernel version 2.6.32

A system call is used by application or user programs to request service from the operating systems. Since the user programs does not have direct access to the kernel whereas the OS has the direct access. OS can access the hardware through system calls only.The following files has to be modified for implementing a system call/usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S/usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h/usr/src/linux-2.6.32.5/include/linux/syscalls.h/usr/src/linux-2.6.32.5/MakefileNew set of files to be createdCreate a new directory newcall/ inside the path “/usr/src/linux-2.6.32.5/” Create new files Makefile, newcall.c and put them in the /usr/src/linux-2.6.32.5/newcall/ folder Create new user files (in any folder of Linux) to test the system call
testnewcall.c, testnewcall.h (created in /home/pradeepkumar) syscall_table_32.S Find the file /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S and add the following line at the end
"…

Electrical Machine Design (equations)

FactorsDC Machine Transformers Induction Machines Synchronous MachinesOutput EquationPa=CoD2Ln, where Pa=P/h for generators, Pa=P for motorsFor Single Phase
Q=2.22 f Bm Ai Kw Aw d10-3
For Three Phase
Q=3.33 f Bm Ai Kw Aw d 10-3Q=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * hQ=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * h
For Turbo alternators
Q=1.11Bavac KwsVa2 L 10-3/nsOutput CoefficientCo=Bav ac* 10-3where Bav-magnetic loading and ac - electric loadingDNACo=11 Kws Bav ac 10-3Co=11 Kws Bav ac 10-3 Choice of Magnetic LoadingFlux Density in Teeth Frequency of Flux Reversals Size of machineDNAMagnetizing current, Flux Density, Iron lossIron loss, Stability, Voltage Rating, Parallel Operation, Transient ShortCircuit current Choice of Electric LoadingTemperature rise,
speed of machine, Voltage, Armature reaction, CommutationDNAOverload Capacity, Copper losses, Temperature rise, Leakage ReactanceCopper loss, Synchronous reactance, Temperature rise, Stray Load losses,
Voltage rating Flux …