PHPMailer attachment, doing it without a physical file
AddStringAttachment($string,$filename,$encoding,$type)
eg
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);
https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addStringAttachment
Embed Image in Mail without having physical Image File
It always helps to read the docs. The function you're looking for is addStringEmbeddedImage
, which does exactly what you want:
$URL = "http://maps.googleapis.com/maps/api/staticmap?center=Albany,+NY&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true";
$image = file_get_contents($URL);
$mail->addStringEmbeddedImage($image, 'staticMap', 'map.png', 'base64', 'image/png');
$mail->Body = '<img width="600" height="300" src="cid:staticMap">';
There are indeed pros and cons to using embedded images, but data urls have especially poor client compatibility, so are generally the worst option. Dynamically linking to the image may not be reliable with google maps, so you may want to proxy and cache the direct map lookups. This will also be much faster than hitting the API every time, and also more reliable as you can deliver an older version if the API doesn't respond or is slow.
use variable's content as attachment in PHPMailer
$mail->AddStringAttachment($string,$filename,$encoding,$type);
http://phpmailer.worxware.com/?pg=tutorial#3
Form a Excel file (PHPExcel) and mail it as an attachment (PHPMailer) without server storing, possible?
To send an attachment without using local files, use the addStringAttachment()
method. For example:
$string = $mything->getExcelData();
$mail->addStringAttachment($string, 'myfile.xls');
Internally this will call PHPMailer::filenameToType()
, which will set the appropriate application/vnd.ms-excel
MIME type for the .xls
extension.
In general you probably don't need to worry too much about memory consumption for doing this - PHPExcel itself is far more memory-intensive than simply storing this string temporarily, so if you are running into trouble, you will likely do so before you ever get this far
PHPMailer Sending just One File
I moved the class creation up to be before the file file processing loop, and moved the attachment code in to the loop
<?php
$msg = '';
use PHPMailer\PHPMailer\PHPMailer;
include_once 'PHPMailer/PHPMailer.php';
include_once 'PHPMailer/Exception.php';
// include_once 'PHPMailer/SMTP.php';
if (isset($_POST['submit'])) {
$inputZip = $_POST['inputZip'];
$selectService = $_POST['selectService'];
$lawnMovingService = $_POST['lawnMovingService'];
$leafRemovalService = $_POST['leafRemovalService'];
$snowPlowingService = $_POST['snowPlowingService'];
$handymanService = $_POST['handymanService'];
$inputName = $_POST['inputName'];
$inputEmail = $_POST['inputEmail'];
$inputPhone = $_POST['inputPhone'];
$inputMessage = $_POST['inputMessage'];
$mail = new PHPMailer; //moved here
if (isset($_FILES['images']['name']) && $_FILES['images']['name'] != '') {
$destination = "attachment/";
foreach ($_FILES["images"]["tmp_name"] as $key => $value) {
$tmp_name = $_FILES["images"]["tmp_name"][$key];
$name = $destination . basename($_FILES["images"]["name"][$key]);
move_uploaded_file($tmp_name, $name);
$mail->addAttachment($name); //attache here
}
} else {
$name = '';
}
//For SMTP
//$mail->Host = "smtp.gmail.com";
//$mail->isSMTP(); // This line may cause problem
//$mail->SMTPAuth = true;
//$mail->Username = "example@gmail.com";
//$mail->Password = "examplePassword";
//$mail->SMTPSecure = "ssl"; //OR TLS
//$mail->Port = 465; //TLS : 587
$mail->addAddress('milan.uptech@gmail.com');
$mail->setFrom($inputEmail);
$mail->Subject = 'Service Booking from Website';
$mail->isHTML(true);
$mail->Body = $inputMessage;
if ($mail->send()) {
header('Location: index.php');
} else {
header('Location: contact.php');
}
// if(sendemail('milan.uptech@gmail.com', $email, $name, $body, $file)) {
// $msg = 'Email Sent!';
// sendemail($inputEmail, 'milan.uptech@gmail.com', $inputName, 'We have received your email');
// }
}
Related Topics
PHP $_Server['Remote_Addr'] Shows Ipv6
Postgres Now() Timestamp Doesn't Change, When Script Works
How to Convert an Integer to an Array in PHP
How to Integrate PHPmailer with Codeigniter 3
How to Use a Like Clause in a Pdo Prepared Statement
Detect How Long It Takes for a File to Upload (Php)
SQL Server Error 1934 Occurs on Insert to Table with Computed Column PHP/Pdo
Count of Duplicate Elements in an Array in PHP
If Statement Within an Array Declaration ...Is That Possible
Serial Comm with PHP on Windows
How to Get the List of Available Locales in PHP
Getting All $_Post from Multiple Select Value
PHP - Syntax Error, Unexpected T_Constant_Encapsed_String