Send PHP mail with Attachment with script and function

In this post we will show you Send PHP mail with Attachment, This custom function or PHP mail attachment script is in a position to send an obvious text email message along with one attachment file. The attachment file has got to be uploaded 1st otherwise you will use a file that already exists on your internet server. There ar far better and a lot of advanced PHP scripts on the web, however I hope this instance can assist you to know, however it’s potential to send associate email message and attachment by victimisation some PHP code.

Send PHP mail with Attachment function mail() operate

PHP mail attachmentThe mail() operate doesn’t support attachment or hypertext markup language mail by default. you wish to use completely different headers and MIME mail components to form this potential. several shared hosting suppliers doesn’t enable the usage of this operate and it’d be disabled.

Normally you may pass 3 values to the mail() operate and some headers. within the example below I skip price|the worth} for the message value, as a result of the message is outlined as a MIME half along with the attachment.

Send PHP mail with Attachment function AddAttachment() operate

Synopsis

require_once ‘Mail/mime.php’;

// set addAttachment function
boolean addAttachment ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $encoding = 'base64' , string $disposition = 'attachment' , string $charset = '' , string $language = '' , string $location = '' , string $n_encoding = null , string $f_encoding = null , string $description = '' , string $h_charset = null )

Description for Send PHP mail with Attachment

Adds associate attachment to a message.

Parameter for Send PHP mail with Attachment

  • boolean $isfile – whether or not $file may be a file name or not.
  • string $file – The file name or the information itself
  • string $c_type – The content form of the image or file.
  • string $charset – The listing of attachment’s content.
  • string $location – The RFC 2557.4 location of the attachment
  • string $name – The urged file name for the information. Only used, if $file contains knowledge.
  • string $description – Content-Description header.
  • string $disposition – The content-disposition of this file Defaults to attachment. attainable values: attachment, inline.
  • string $n_encoding – encryption of the attachment’s name in Content-Type By default filenames area unit encoded exploitation RFC2231 technique Here you’ll set RFC2047 encryption (quoted-printable or base64) instead.
  • string $encoding – form of transfer encryption to use for the file knowledge. Defaults is “base64”. For text based mostly files (eg. scripts/html etc.) this might lean as “quoted-printable”.
  • string $h_charset – The listing of the headers e.g. file name If not nominal, $charset are used
  • string $f_encoding – encryption of the attachment’s file name in Content-Disposition header.
  • string $language – The language of the attachment

Return valuses for Send PHP mail with Attachment

boolean – Returns TRUE on success, PEAR_Error on failure

// Send PHP mail with Attachment
require 'class.phpmailer.php';

$mail = new PHPMailer;

$seller_name = "onlinecode test";
$temp = "temp/onlinecode.pdf";
$poorder = "purchase-order-agreement.pdf";

$seller_email_id = "ingo@onlinecode.com";
$mailto = $seller_email_id;

$subject = "Notification of order agreement";
$bodytext = "<br> Hello<br> <b>".$seller_name."</b>,<br>This Order has been sent to the Seller for their review. You will be notified when their review has been completed. and this test for Send PHP mail with Attachment<br> Regard <br> <b>onlinecode Team</b>  ";

$mail->IsSMTP();                                      // Set mailer to use SMTP
//$mail->SMTPSecure = 'tls';
$mail->Host = 'ssl://smtp.gmail.com';                // Specify main and backup server
$mail->Port = 465;  
$mail->SMTPDebug = 1;                                  // Set the SMTP port
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'onlinecode12365@gmail.com';                // SMTP username
$mail->Password = 'ADD-YOUR-PASSWORD';                  // SMTP password
//$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted
$mail->AddAttachment($temp, $poorder);
$mail->From = 'onlinecode@onlinecode.org';
$mail->FromName = 'onlinecode Team';// frome name
$mail->AddAddress($mailto , $seller_name);  // Add a recipient  to name 
//$mail->AddAddress($mailto );               // Name is optional


$mail->IsHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    = $bodytext;

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
else
{
	echo 'Message has been sent';
}