I trying to send email using the PHP mail function but when I use fake email and it gives me no error and I cannot find out the email was sent or not I try all the ways from other similar question answers but I don't get what I want
I need to get a delivery report
my PHP version is 7.3
my PHP codes:
$to = 'example#somesite.com';
$subject = 'Message from ';
$message = $Message ;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'To: '. $to;
$headers[] = 'From: <sombody#theresite.com>';
if(mail($to, $subject, $message, implode("\r\n", $headers))){
echo 555;
}else{
echo 444;
}
Is there any way to get message for successful sent or fail?
For delivery confirmations:
You have to add the Disposition-Notification-To header.
For usage details see RFC 3798.
General tip for stuff like this:
Use the mail client of your choice to generate an e-mail with the desired parameters, send it to yourself and look at the mail source.
There you can find the necessary headers added by the feature you are looking for. Then read the RFC or google for specific details on the header in question.
Ref: Delivery reports and read receipts in PHP mail
Related
Well I am using the PHP MAIL function and for some reason every email it sends has a weird;
This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";
PHP mail sending problem when using a tag, it doesn't come to new line.
HERE is my code having same problem
$subject = 'Watch Out Our Colorful Web Design Presentation';
$headers = "From: " . $email . " \r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: test#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "Watch Out Our Colorful Web Design Presentation.\r\n";
$message .= "<a href='http://www.stackoverflow.com'>CLICK HERE</a>\r\n";
mail($to, $subject, $message, $headers);
Mail send successfully but having problem in \r\n. It doesn't take new line. I tried br tag too. But it goes in junk mail.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You're sending a HTML email. This means that you should be using HTML instead of newlines. To avoid having your emails placed in the junk folder, you should read some of the many Stackoverflow topics on the subject.
You have to use <br> tag for new line.
You can't use \n for new line for html printing.
https://bugs.php.net/bug.php?id=9542
As others have said, you need to be using HTML line breaks <br /> if you are sending an e-mail with Content-Type: text/html. The newlines/carriage returns will be interpreted in the source of the message as line breaks but they will probably not be rendered as HTML.
When sending e-mail from PHP I would always suggest using an e-mail class rather than PHP's native mail functions.
I tend to use SwiftMailer. It has the advantage that all mail headers are sanitized and escaped to avoid header injection exploits that could potentially fire out spam through your script. Also it's easier to use a variety of e-mail transports. There's also a great decorator plugin which can send thousands of messages with customised strings, useful for doing things like "Dear {first_name} {surname}" or customised unsubscribe/tracking links.
Here's some sample code for SwiftMailer just in case you are interested...
// START SWIFTMAILER
require_once($swiftmailer_path);
$swift_transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$swift = Swift_Mailer::newInstance($swift_transport);
$swift_msg = Swift_Message::newInstance($swift_transport);
$swift_msg->setMaxLineLength(150);
$swift_msg->setFrom( array('NoReply#domain.com' => 'MyWebsiteName'));
$swift_msg->addTo($user);
$swift_msg->setSubject($subject);
$swift_msg->setBody($msg_html, 'text/html');
$swift_msg->addPart($msg_txt, 'text/plain');
// SEND E-MAIL
if ($swift_result = $swift->send($swift_msg)) {
// SENT SUCCESSFULLY
} else {
// ERROR - E-MAIL NOT SENT
}
you can send a status update to a facebook page you own by sending it to a cretin (secret) email address. this is described here:
http://www.facebook.com/help/pages/mobile
the problem is I cant make it work from php
function page_publish_by_mail($page_mail, $status){
$to = $page_mail;
$subject = $status;
$message = "";
$headers = "From: my#mail.address";
return mail($to, $subject, $message, $headers);
}
I can send mail to my email address and I can post by mail from my email address but I can't seem to post by mail from PHP.
I haven't tried to send to facebook mail before, however I feel like it is being filtered out due to lack of header information. Try adding some more header details.
I always send headers like this:
$headers = 'From: Your Name <youremail#domain.com>' . "\r\n";
$headers .= 'Content-type: text/html' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
This is set up to send an html email, but you might try other content-types if that doesn't work.
It may be a good idea to look in the documentation and see if there are other headers that are required as well.
i'm setting up a system that whenever a user registers on my site it will automatically send a confirmation email.
My problem is that the email that is using ends with info#buick.websitewelcome.com which is weird because my website is not buick.websitewelcome.com.
My expected email should be info#myaddress.info.
How would I do that, i'm using CPanel FYI.
This is also my code when sending the confirmation address
function SendUserEmailVerificationCode( $code,$username,$email_address ) {
// multiple recipients
$to = $email_address;
// subject
$subject = 'Verification Code';
// message
$message = "
Dear ".$username."<br><br>
You received this email because you registered at http://myaddress.info/.<br>
To complete registration please enter this registration code<h1>".$code."</h1></strong> on the link below:<br>
http://myaddress.info/verifyMe.php?username=".$username."
";
$headers = 'From: info#myaddress.info' . "\r\n";
// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
}
Any help would be greatly appreciated and rewarded!
Try the command line paramter -f as shown on http://php.net/manual/en/function.mail.php#example-3048
<?php
mail('nobody#example.com', 'the subject', 'the message', null,
'-fwebmaster#example.com');
?>
The fourth argument in the mail() function is used for headers as you have already noticed. Just add a From header. This will change the from field.
$headers .= 'From: info#mywebaddress.com'."\r\n";
Additionally, if you also want to change the envelope mail from (which you probably want), you can use the fifth argument. This is used for options that should be passed directly to sendmail. Here, you should add -f info#mywebaddress.com. A simple example is shown below.
mail('recipient#domain.com', 'Subject', 'Message',
'From: info#myaddress.info','-f info#myaddress.info');
And also, all of this is mentioned in the official PHP manual on mail().
-f will set the From address, -r will override the default Return-path that sendmail generates (typically the From address gets used).
Ref: PHP Manual: mail
Is the following "From" header incorect?
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Mail Master <mail#mailmaster.com>' . "\r\n";
if(sendEmailNow($email, $subject, $body, $headers)){
I get the error from my mail server. It says "Mail from error: Syntax error".
Thanks all for any help.
Update
I have stripped down the SendEmailNow function to the below and I get the same error:
//send an email
function sendEmailNow($email, $subject, $body, $headers){
if (mail($email, $subject, $body, $headers)) {
##check email
##code to say email sent - compare with the number registered
return true;
}
else {
##code to report an error
return false;
}
}
Update 2
Problem solved. I am running this on a windows machine using PHP 5. Like the correct answer chosen and the comments have said. Some mail servers have trouble understanding what I had previously. But what worked for me was this:
$headers .= 'From: mail#mailmaster.com' . "\r\n";
A Google search for the error message suggests that some SMTP servers fail to parse your syntax for the From header. Can you try the following syntax to rule out this possibility?
From: mail#mailmaster.com
Unless the body is empty, you may need an additional CRLF to terminate the headers. Without knowing the API I can't say much more.
Not sure if this'll help, but here's what the output headers look like in my Python app in pure-text:
Content-Type: multipart/alternative;
boundary="10.254.26.130.1.1364.1241389770.060.1"
From: User1 <user1#domain1.com>
To: User2 <user2#domain1.com>,User3 <user3#domain.com>
Subject: Actual subject
MIME-Version: 1.0
--10.254.26.130.1.1364.1241389770.060.1
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
actual text content starting here...
Again, without knowing all the content/headers it's hard to say, but I'd guess either a) You've left out the trailing CRLFs before the content, or b) one of the earlier headers missing its CRLF.
Apologies if that takes you in a wildly wrong direction. :)
From: "User1" <user1#domain1.com>
The From header requires quotes for the name part.
If you set the "$userEmail" variable in your form, you can have it from them and a reply to. The $email_id is your email it is sent to.
$to=$email_id;
$headers = "From: " . strip_tags($userEmail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($userEmail) . "\r\n";