Attachment is going to Gmail but not other mailboxes - php

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.

Related

PHP Mail BCC recpients ignored

I am struggling this BCC.
I have 2 email addresses that I've included in the BCC field, but only the first email in the field gets the message, the second email is ignored.
i don't know what am doing wrong.
my code is below
$EmailListHere = 'mail1#domain.com,mail2#domain.com';
$headers = "From: ".$_POST['from-address']."\nX-Mailer: php\n";
$headers .= "MIME-Version: 1.0\n"; #Define MIME Version
$headers .= "Content-Type: text/html; charset=ISO-8859-1\n"; #Set content type
$headers .= "Bcc: $EmailListHere\n"; #Your BCC Mail List
echo mail(null, $subject=$_POST['subject'], $message=$_POST['html-body'], $headers);

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.

Output mutiple attatchments with PHP mail [duplicate]

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

Mulitpart MIME email text and HTML shows both parts

I've been banging my head on this one for awhile and have been unable to find any helpful articles on my issues. I'm writing a PHP site using the built in mail function for some quick confirmation emails. I realize that there is a fair amount of prejudice against the built in mail function but it has been working well for me up to this point and I would like to be able to continue using it. When I send just a plain text email it all works great, as if I send just a HTML email. However if I try to do a multipart Text/HTML email both versions show up in my email client (tried both thunderbird and gmail). I'm hoping someone here can help me figure out what I'm doing wrong (besides using mail() instead of PHPMail). Here is my code snippet
$uid = md5(uniqid(time()));
$strSubject = "Confirmation for $strEventName on $strEventDate";
$strHTMLMsg = "<h1><center>You are confirmed for the following event:</center></h1><br>\n$strEvenDetails";
$strMsg = strip_tags($strHTMLMsg);
$toEmail = "\"$strName\" <$strEmail>";
$header = "$FromEmail\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= "This is a MIME encoded message.\n\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/plain; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $strMsg."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/HTML; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $strHTMLMsg."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/ics; name=\"".$strFileName."\"; method=PUBLISH; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= "Content-Disposition: attachment; filename=\"".$strFileName."\"\n\n";
$header .= $strICSEvent."\n\n";
$header .= "--".$uid."\n";
$bSuccess = mail($toEmail,$strSubject,"",$header);
Necrobump, but I arrived here after googling, so for others with the same search terms: multipart/mixed implies that you will view all parts. For both HTML and text parts where only one should be visible, use multipart/alternative instead. The last part gets the highest priority.
Also see Mail multipart/alternative vs multipart/mixed for more info, and info about "stacking" mime content.

PHP Disposition-Notification-To

I'm trying to get a read receipt from my PHP emails, here's my code:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: ABC <do-not-reply#abc.com> \n";
$headers .= "Disposition-Notification-To: abc#hotmail.com \n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
I tried opening the email on my phone and outlook. In both cases, received no email to confirm that the email was open.
I also tried
$headers .= "Read-Receipt-To: abc#hotmail.com \n";
And
$headers .= "X-Confirm-Reading-To: abc#hotmail.com \n";
Read receipts are entirely up to the receiver, there is no way to guarantee one, and many webmail clients will explicitly ignore this for privacy reasons.
Try using Outlook (the office suite application, not the webmail/hotmail clone, not sure which you used) and seeing if you get a popup that says "do you want to send a read receipt? yes / no"

Categories