I stored the images in url format in database.now i want to send mail with multiple images using mail() in php
$sql11 = "SELECT `photo_id`, `project_id`, `map_id`, `map_flag`, `user_id`, `photo_path`, `pdf_path`, `created_at`, `modified_at` FROM `photos` WHERE map_flag='Task' and map_id='$task_id'";
$rl = $conn->query($sql11);
while ($res11 = mysqli_fetch_assoc($rl)) {
$array[] = [$res11['photo_path']];
}
$str = implode(',', $array);
$message = $message_content . "\n" . "\n" . "\n" . $message1 . "\n" . $message2 . "\n" . $message3 . "\n" . $message4 . "\n" . $message5 . "\n" . $message6 . "\n" . $array . "\n" . $message8 . "\n";
$from = "123#test.com";
$headers = 'From: ' . $from . "\r\n";
$result = mail($to, $subject, $message, $headers);
First off, you need to tell the email app that this is html. This can be done by adding the line
$headers .= "Content-type: text/html; charset=UTF-8";
Next, what is photo_path? Does it also include the full url or a relative url? You need to set the full url i.e. http://example.com/{$res11['photo_path']}
Related
This question already has answers here:
How to send UTF-8 email?
(3 answers)
Closed 3 years ago.
I have a PHP form on my website. It collects text strings from HTML input elements, then sends the text to my mailbox. However, when people type in text in other languages, the email appears garbled. The text appears to be okay in the Web browser. There's even some JavaScript code that gets/sets some of these form fields, and seems to do so correctly.
Maybe the PHP code is not treating the text as unicode? Or the text gets sent to my mailbox using the wrong encoding? I would appreciate some help, thanks.
I have already added
header("Content-Type: text/html; charset=utf8");
to the top of the PHP files.
Here is a part of the PHP form that gets the inputted values using POST.
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$titletxt = $_POST["titletxt"];
$titleurl = $_POST["titleurl"];
$legend = $_POST["legend"];
$command = $_POST["command"];
$binding = $_POST["binding"];
$layout = $_POST["layout"];
Here is a part of the PHP form that emails me the text:
mail
(
"blahblah#blah.com",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n"
);
Please, try to add utf-8 charset header to your mail function, for example
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <example#example.com>' . "\r\n";
$headers .= 'Cc: example#example.com' . "\r\n";
mail
(
"blahblah#blah.com",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n",
$headers
);
The header() function sends a raw HTTP header to a client browser, but mail function with the variable $headers sends headers to email.
I'm trying to use a PHP email form I found online that supports attachments, with a bit of extra code, but it's not working, I get a message saying "Sent", but no emails (I checked spam already).
Here is the php file:
function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = #mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'someEmail#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['files'];
$subject = 'Customer Contact From Service Form';
$html_content = $message;
//call multi_attach_mail() function and pass the required arguments
$send_email = multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1><br>".$message:"<h1> Mail not SEND</h1>";
My form looks like this:
<form NOVALIDATE action="processContact.php" enctype="multipart/form-data" id="claimsForm" method="post" name="claimsForm">
..lots of fields...
<div class="form-group">
<label class="control-label claimsForm-field-header" for="customerTry">Photos:</label>
<input type="file" id="files[]" name="files[]" multiple="multiple" />
</div>
<input class="green-btn" id="ss-submit" name="submit" type="submit" value="Submit">
</form>
The function came with the form I downloaded, so I don't think there is a problem with it. I've been playing around with the code and trying different things, but no luck (I'm not a PHP guy).
EDIT:
Based on the suggestion below, I tried using phpMailer, following the example they have, here is my php file now:
/**
* PHPMailer simple file upload and send example
*/
$msg = '';
$message = "Customer Contact"
. "\n" . "Name: " . $_POST['fullName']
. "\n" . "Email: " . $_POST['email']
. "\n" . "Phone: " . $_POST['phone']
. "\n" . "Current Phone: " . $_POST['currentPhone']
. "\n" . "Address: " . $_POST['address']
. "\n" . "Retailer: " . $_POST['retailer']
. "\n" . "Product Type: " . $_POST['productType']
. "\n" . "Specific Item: " . $_POST['item']
. "\n" . "Purchase Date: " . $_POST['purchaseDate']
. "\n" . "Invoice Number: " . $_POST['invoiceNumber']
. "\n" . "Issue: " . $_POST['issue']
. "\n" . "How did the issue happen?: " . $_POST['how']
. "\n" . "When did the issue occur?: " . $_POST['when']
. "\n" . "Have you done anything to try correcting the issue?: " . $_POST['customerTry'];
$to = 'murtorius#gmail.com';
$from = $_POST['email'];
$from_name = $_POST['fullName'];
//attachment files path array
$files = $_FILES['userfile'];
$subject = 'Customer Contact From Service Form';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require 'php-mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom($from, $from_name);
$mail->addAddress($to, 'TEST');
$mail->Subject = $subject;
$mail->msgHTML($message);
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'Photos');
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
I changed the input file field name to 'userfile[]', but I get this:
Warning: sha1() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 35
Warning: move_uploaded_file() expects parameter 1 to be string, array given in /home/zucora/service.zucora.com/processContact.php on line 36
If I change the name to 'userfile' (without []) I get a blank screen and no email.
I am sending a mail after user submits form to approve are disapprove the data through link in mail. Here is the code:
$subject = 'User needs approval';
$message = 'The user $empname needs your approval' .
'----------------------------------- ' . "\r\n" .
'Accept: ' . $accept_link . "\r\n" .
'Decline: ' . $decline_link . "\r\n";
$_headers = 'From:admin#yourwebsite.com' . "\r\n"; // Set FROM headers
//mail($supemail, $subject, $message, $headers); // Send the email
fnMail($supemail,"mailb#mail.com"," Web master","reimbursement Form",$message);
The link to accept and decline is:
$accept_link = "http://communique/elegant-contact-form/quick-contact/ajax/approval.php?e=" . $empemail . "&h=" . hash('sha512', 'ACCEPT');
$decline_link = "http://communique/elegant-contact-form/quick-contact/ajax/approval.php?e=" . $empemail . "&h=" . hash('sha512', 'DECLINE');
and here is the approval.php file:
$hash = $_GET['h'];
$email = $_GET['e'];
if($hash == hash('sha512', 'ACCEPT')){
$res=execQuery('oth','update form set approved=1 where empemail="$email"');
if(count($res)>0){
fnMail($email,"mk.web#mattsenkumar.com","MattsenKumar Web master","Contact Front registration",'APPROVED');
}
}else if($hash == hash('sha512', 'DECLINE')){
fnMail($email,"mk.web#mattsenkumar.com","MattsenKumar Web master","Contact Front registration",'DECLINE');
}
But I got the mail like this:
The user $empname needs your approval----------------------------------- Accept: Decline:
This is day-1 basic PHP syntax: variables are not interpolated into single-quoted strings. Read the docs, do this:
$message = "The user $empname needs your approval" .
For the accept and deny links to be included in the message you must define them before you use them, like this:
$accept_link = "http://communique/elegant-contact-form/quick-contact/ajax/approval.php?e=" . $empemail . "&h=" . hash('sha512', 'ACCEPT');
$decline_link = "http://communique/elegant-contact-form/quick-contact/ajax/approval.php?e=" . $empemail . "&h=" . hash('sha512', 'DECLINE');
$message = "The user $empname needs your approval" .
'----------------------------------- ' . "\r\n" .
'Accept: ' . $accept_link . "\r\n" .
'Decline: ' . $decline_link . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
Try setting the headers
or check this below stackoverflow link
hope this will help
PHP mail() - How to put an html link in an email?
This is on a LAMP server.
All the answers I've found online suggest that one of the above should work, but I cant get it working.
My code is:
$to = $email_to;
$subject = 'Website Form';
$message = 'From: ' . $name .
' \r\n Date: ' . $date .
' \r\nText: ' . $text .
'\r\n Use on Presentations?: ' . $presentations .
'\r\n Use on Websites?: ' . $website .
'\r\n Use on Case Studies?: ' . $casestudy;
$headers = 'From: website#website.com' . "\r\n" .
'Reply-To: no-reply#website.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Would anyone know what I've done wrong?
You are using single quotes, if you want to use \n or \r you have to put them in double quotes, like:
$value = "here is some text, and a $variable, and a line end\r\n";
I do a lot of php mailer stuff and \r\n has to be in "..." or it's not executed as an escaped character, but rather as a literal.
Ie:
echo ".\n.";
# is this:
.
.
#but
echo '.\n'.;
#is this:
.\n.
that's all.
For that you can set header like $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; and then user <br /> in place of \n.
Why are you using \r\n, use <BR>
$message = 'From: ' . $name .
' <BR> Date: ' . $date .
' <BR>Text: ' . $text .
' <BR>Use on Presentations?: ' . $presentations .
' <BR>Use on Websites?: ' . $website .
' <BR>Use on Case Studies?: ' . $casestudy;
I am using the following code
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = "<html><body><p>".$message."</p></body></html>";
$this->Mail($storeEmail, $subject, $message);
function Mail($to, $subject, $message)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: The Vow Engagement Ring Finder <thevow.engagement#gmail.com>' . "\r\n";
// Mail it
if(!mail($to, $subject, $message, $headers)) {
throw new Exception('There was a problem trying to send an email.');
}
}
The problem is all is i get is one paragraph. I have added <br>s but its like they don't work. The mail I get is simple paragraph without any new line feeds.
Mistake in <br /> tags
$message = $mess0 . "</br>" . $mess1...
^^^^^^
Replace all </br> with <br />
It will not work. You have to use \r\n for line breaks. Also try <br />.