Php mengirim email dengan contoh lampiran pdf

Skrip PHP untuk mengirim email dengan lampiran


Selain fungsi surat php default chunk_split, base64_encode dan file_get_contents juga digunakan dalam skrip ini.
$htmlbody = " Your Mail Contant Here... You can use html tags here...";
$to = "[email protected]"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents('logo.png')));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"logo.png\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed"; ?>


Klik di sini untuk mengirim mengirim surat teks, surat HTML menggunakan fungsi surat PHP

Unduh berkas

Unduhan Total. 15564

Terkadang kami mungkin perlu mengirim email dari situs web kami ke klien dengan beberapa lampiran. Dalam tutorial ini kita akan menanyakan database MySQL untuk mendapatkan data yang diperbarui dari tabel kita dan mengirimkan data tersebut ke email yang diinginkan sebagai lampiran PDF

Demo Langsung

PHP Kirim Email Unduh proyek ini dari Github

Untuk ini kita akan menggunakan

  • AngularJS [Untuk ujung depan]
  • PHP [Untuk menanyakan database dan mengirim email]
  • MySQL [Database]
  • perpustakaan fPDF [Ini akan menghasilkan PDF untuk kita]
  • SendGrid [Ini membantu kami mengirim email dengan cara yang lebih baik]

kode

indeks. html

kelasDatabase {pribadi. $koneksi->connect_error . kosong($set hasil) class Database {private $host = "localhost";private $user = "root";private $password = "root";private $database = "crud";function runQuery($sql) {$conn = new mysqli($this->host,$this->user,$this->password,$this->database);if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}$result = $conn->query($sql);if ($result->num_rows > 0) {while($row = $result->fetch_assoc()) {$resultset[] = $row;}}$conn->close();if(!empty($resultset))return $resultset;}}?>

lihat mentah yang dihosting dengan ❤ oleh GitHub

buku. sql

---- Struktur tabel untuk tabel `buku`--CREATE TABLE IF NOT EXISTS `books` (`name` varchar(255) DEFAULT NULL,`author` varchar(255) DEFAULT NULL) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;---- Dumping data for table `books`--INSERT INTO `books` (`name`,`author`) VALUES('What young India wants', 'Chetan Bhagat'),('Two States', 'Chetan Bhagat'),('The hunger games', 'Suzanne Collions'),('The 3 mistakes of my life', ' Chetan Bhagat'),('Serious Men', ' Manu Joseph'),('Revolution 2020', ' Chetan Bhagat'),('God"s Little Soldier', 'Kiran Nagarkar');

lihat mentah yang dihosting dengan ❤ oleh GitHub

aplikasi. js

'gunakan ketat';angular. modul('sendmailApp', []).pengontrol('MailController', function ($scope,$http) {$scope.memuat = false;$ . .surat = {ke . 'support@codenx. com'};$cakupan. kirim = fungsi (email . ){$scope.memuat = benar;$ . .postingan('api/index. php', { ke . email. kepada }). kemudian(res=> . $scope.memuat = false;if . (res.status===200)< . .serverMessage = 'Email dikirim dengan lampiran';else$scope.serverMessage = 'Kesalahan mengirim email';});}})

lihat mentah yang dihosting dengan ❤ oleh GitHub

config. php

?phpdefinisikan ( 'DATABASE_HOST' , . pdf" "localhost");define('DATABASE_NAME', "demos");define('DATABASE_USERNAME', "root");define('DATABASE_PASSWORD', "root");define('ATTACHED_FILENAME', "books.pdf" ); definisikan ( 'SENDGRID_USERNAME'< . com"< . com", "YOUR_SENDGRID_USERNAME");define('SENDGRID_PASSWORD', "YOUR_SENDGRID_PASSWORD");define('FROM', "[email protected]" ); definisikan ( 'TO', "[email protected]" ); define ( 'SUBJECT', "ShopNx - The Single Page eCommerce Website");define('CONTENT', "

Experience faster shopping with ShopNx

  • Responsive Design
  • Higher Scalability
  • Ergonomically Designed
"
);?>

lihat mentah yang dihosting dengan ❤ oleh GitHub

basis data. php

kelasDatabase {pribadi. $koneksi->connect_error . kosong($set hasil) class Database {private $host = "localhost";private $user = "root";private $password = "root";private $database = "crud";function runQuery($sql) {$conn = new mysqli($this->host,$this->user,$this->password,$this->database);if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}$result = $conn->query($sql);if ($result->num_rows > 0) {while($row = $result->fetch_assoc()) {$resultset[] = $row;}}$conn->close();if(!empty($resultset))return $resultset;}}?>

lihat mentah yang dihosting dengan ❤ oleh GitHub

indeks. php

termasuk('config. php');termasuk('database. php');$email = DI MANA `TABLE_SCHEMA`='demo'DAN `TABLE_NAME`='buku'dan `COLUMN_NAME` . php');$pdf = );$hasil = include('config.php');include('database.php');$email = TO;if(isset($_POST['email'])) $email = $_POST['email'];$database = new Database();$result = $database->runQuery("SELECT name,author FROM books");$header = $database->runQuery("SELECT UCASE(`COLUMN_NAME`)FROM `INFORMATION_SCHEMA`.`COLUMNS`WHERE `TABLE_SCHEMA`='demos'AND `TABLE_NAME`='books'and `COLUMN_NAME` in ('name','author')");require('fpdf/fpdf.php');$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial','B',16);foreach($header as $heading) {foreach($heading as $column_heading)$pdf->Cell(95,12,$column_heading,1);}foreach($result as $row) {$pdf->Ln();foreach($row as $column)$pdf->Cell(95,12,$column,1);}$pdf->Output(ATTACHED_FILENAME,'F');require('sendgrid.php');$result = sendmail($email);echo $result;?>

lihat mentah yang dihosting dengan ❤ oleh GitHub

sendgrid. php

termasuk('config. php');fungsisendmail(;$filePath = ATTACHED_FILENAME. ']' => '@'. $filePath. '/'. ATTACHED_FILENAME);$permintaan . 'api/mail. mengirim. json';// Buat permintaan curl$sesi include('config.php');function sendmail($email){$url = 'https://api.sendgrid.com/';$filePath = dirname(__FILE__);$params = array('api_user' => SENDGRID_USERNAME,'api_key' => SENDGRID_PASSWORD,'from' => FROM,'to' => $email,'subject' => SUBJECT,'html' => CONTENT,'files['.ATTACHED_FILENAME.']' => '@'.$filePath.'/'.ATTACHED_FILENAME);$request = $url.'api/mail.send.json';// Generate curl request$session = curl_init($request);// Tell curl to use HTTP POSTcurl_setopt ($session, CURLOPT_POST, true);// Tell curl that this is the body of the POSTcurl_setopt ($session, CURLOPT_POSTFIELDS, $params);// Tell curl not to return headers, but do return the responsecurl_setopt($session, CURLOPT_HEADER, false);// Tell PHP not to use SSLv3 (instead opting for TLS)curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);curl_setopt($session, CURLOPT_RETURNTRANSFER, true);// obtain response$response = curl_exec($session);curl_close($session);// return the executation statereturn $response;}?>

lihat mentah yang dihosting dengan ❤ oleh GitHub

# php # pdf # email

Bagaimana cara mengirim email dengan lampiran file di php?

Fungsi PHP mail() dengan beberapa header tipe MIME dapat digunakan untuk mengirim email dengan lampiran di PHP . Dalam kode contoh berikut, header MIME dan Content-Type digunakan dengan fungsi mail() untuk mengirim email dengan lampiran menggunakan PHP. $to – Alamat email penerima. $dari – Alamat email pengirim.

Bagaimana cara mengirim file PDF menggunakan PHPMailer?

Bagaimana cara mengirim file pdf melalui phpmailer? .
Buat direktori untuk proyek di direktori htdoc di xampp jika Anda menggunakan server wamp maka Anda membuat direktori di direktori www
2. Buka direktori proyek Anda dengan command prompt setelah itu tulis perintah ini. -
komposer memerlukan mpdf/mpdf

Bagaimana cara mengirim lampiran di PHPMailer?

Lampiran Berkas . Baris tunggal ini akan menambahkan lampiran ke email Anda. $path adalah jalur dari nama file. Itu bisa relatif (dari skrip Anda, bukan kelas PHPMailer) atau path lengkap ke file yang ingin Anda lampirkan. mail->Send(); and it's called AddAttachment($path); . This single line will add the attachment to your mail. $path is the path of the filename. It can be a relative one (from your script, not the PHPMailer class) or a full path to the file you want to attach.

Bagaimana cara mengirim file Excel yang dihasilkan sebagai lampiran dalam email dari php?