Output mutiple attatchments with PHP mail [duplicate] - php

This question already has answers here:
Sending multiple attachment in an email using PHP
(2 answers)
Closed 7 years ago.
I wonder if anyone can help me? I have done a lot of research on outputting multiple attachments using PHP mail but I just cant quite get my upload form working correctly.
Background:
I have a form which allows you to upload multiple images. The form itself is fine and i have had working with one one image no problem. The form sends a e-mail with an attachment. The problem has arisen when trying to send multiple attachments.
I can only ever get it to send one.
I have a piece of code below which deals with going through the array of files and adding them to headers of the e-mail. (see Below)
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "From: Sell My Rolex <info#sellmyrolex.today>\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $message."\r\n\r\n";
$x="0";
foreach($files as $file){
$handle = fopen($destination.$file, "r");
$content = fread($handle, filesize($destination.$file));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$headers .= "Content-Type:".$types[$x]."; ";
$headers .= "name=\"".$file."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $attachment."\n\n";
}
This it is sent out
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers);
However all i get in the e-mail above the main e-mail message is
Content-Type:image/png; name="image1.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="image1.png"
X-Attachment-Id:6299
--f2b57013356e3f84c2c6b1d9b77f95d2--Content-Type:image/png; name="cleanoffice.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="cleanoffice.png"
X-Attachment-Id:5833
Can anyone point me in the right direction? I know people may want to suggest using a library like PHPmailer rather than the basic php mail but i want to use php mail to get this complete.
Any help is much appreciated.
Thanks

You need to include multiple boundaries to mark different attachments. The following gives you a good example of how it would look like.
http://www.qcode.co.uk/post/70

Related

assist with inserting a picture into an html email using php

Hello: been doing html emails with php mail() without any problems - but having a heck of time trying to add a picture properly. I load in the content of the email into a variable called $messageContent
here is my actual calling of the mail() function:
$headers = "From: BBT Admin - " . $emailFrom . "\r\n";
$headers .= "Reply-To: ". $emailFrom . "\r\n";
$headers .= "BCC: ".$bccEmail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($emailTo,$subject,$messageContent,$headers);
All of this works fine - only have problems depending on what I put in the "content" variable...
I basically keep appending to the content variable - building the content one piece at a time...here is the part that is the problem:
$messageContent="";
$messageContent.="<a
href='http://www.example.com'>example.com</a>";
$messageContent.="<br style='clear:both;'/>";
$messageContent.="<br/><br/>";
$messageContent.="<img src='http://example.com/wp-content/uploads/2018/03/Capture_bbtmap3.png'/>";
So this doesnt work that well - so if anyone can help on this it would be greatly appreciated!

Email is not coming in Gmail inbox using this php code [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using this code for my contact form in a html website but mail is not coming in Gmail Inbox.
Can any one help me i am trying to solve this issue but i don't have any guide.
<?php
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "iamuser#gmail.com"; //Recipient's E-mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];
if (#mail($to, $subject, $message, $headers))
{
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
// header('Location: ../index.html');
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
There are some problem in setting the header.
And most important is that you need to define the correct and valid Email Id in the From section because google generally used to validate the domain, the mail coming from.
If it is not white-listed at google end then it wills end the mail to Spam automatically, that is happening now as I think.
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are
You can try the below code.
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
Reference Link.
https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer
There is another code you can try:
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain;charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n";
// use different content types here$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK";
// or use booleans here} else {echo "mail send ... ERROR!";
}
}
If you are developing this program in local server. The emails will not be sent to your gmail account.
If you want to test your code on a local machine please install Test Mail Server Tool.
No email will be delivered while running on local machine but you will get an idea how the email will look like.
When you run the same on web hosting server, the email will be delivered to the email id specified in $to field.

Attachment is going to Gmail but not other mailboxes

These are the headers which I am using to send email with an attachment, however the attachment only gets through to Gmail. For all other mailboxes I tried, only the message content arrives, without the attachment.
# Define the main headers
$header = "From:$from_email1\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/html\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message1\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"$cv_view12\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject1, $message1, $header );
What's the size of the attachment?
Gmail has a 25mb attachment limit, which is a lot larger than many email providers.
Now, I'd expect you'd get a bounce-back message telling you that the attachment is too big if this is the case, but if the email address you're using for "From" is not valid, this wouldn't bounce back to anywhere you could see.
I'd start by setting a valid "From" address and see if you get any messages back as to why.
I also highly recommend Swift Mailer:
http://swiftmailer.org/
Which takes a lot of the complexities out of sending mail, and may automatically fix a small typo in manually creating a mail message. There is also a lot of advanced functionality built-in which makes life much easier.

Can we send HTML emails using PHP? [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I have been working on a email script and it works. Prob is I do it using a textarea . can I send an HTML email?if so , how?
Edit: Found the answer
Ofcourse you can. You have to set headers for that
$to = 'abc#example.com';
$subject = 'My Subject';
$message='<b>test</b>';
$headers = "From: test#test.com\r\n";
$headers .= "Reply-To: test#test.com\r\n";
$headers .= "CC: test2#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);

PHP mail function not working based on the body of the message

so on my server I tried running:
mail('my#email.com', 'asdf', 'asklfdjksalfdsdaf I know I I know I');
and PHP sent the mail perfectly....but then when I changed the message to
mail('my#email.com', 'asdf', 'hahahahaa');
it did NOT send the email to me....
what can possibly cause this? the only thing that's different is the message body...I am completely baffled...
Here's the way I send messages with php and avoid sending to spam folder:
<?php
$to = "test#test.com";
$subject = "Test Email";
$message = "Test Email";
// normal headers
$num = md5(time());
$headers = "From: Mailer <mailer#test.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".time()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
#mail($to, $subject, $message, $headers);
?>
You should check your spam filter. It is possible that it is catching the message as being too short or not having enough discernible words, or any one of the other hundred things they'll check for.
If it is the spam filter, another thing that will help is adding different headers to help validate the email. Sometimes simply adding a realistic FROM in the headers can allow it through a spam filter. There's a default FROM set, usually in your php.ini but if it's some system generated name then filters can flag it.
http://php.net/manual/en/function.mail.php
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Categories