Attach File Through PHP Mail - php

I am trying to get a custom contact form using PHP mail to have a user attach a photo, that then gets sent to the recipient outlined in the PHP mail code
<input type="file" id="file" name="file">
The form code is as follows;
<form action="register-mail.php" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit" value="Submit">
</form>
The PHP mail code is as follows;
<?php $file = $_FILES['file'];
$formcontent="Email Text Content";
$recipient = "fake#email.com";
$subject = "Here is a Photo";
$mailheader = 'From: Basic Sign-up <fake#email.com>' . "\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
die();
?>
I can't seem to get it to attach the file to the email being sent. What am I doing wrong?

That is not how attachment works. Using the mail() for attachments is a little more complex than that. You got to tell mail() which part should handle the file attachment and which part is responsible to display the email body by setting up a MIME Boundary. In other words, the code should be divided into 2 parts:
A section to handle the message being sent in body
A section to handle file uploading
A detailed tutorial is here
PHP EMAIL WITH ATTACHMENT
However, I would suggest you to use a very handy tool called PHPMailer to do the same task. It simplifies the process and lets the class handle all the legwork.
PHPMailer

Related

Form isn't sending email upon submission. What am I missing? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm fairly new to php and I'm still learning the basics. I created a simple "contact us" form that should send the data to an email address. However, I'm not receiving the email. The "Thank you" message displays correctly, but the email is never sent.
Unfortunately my knowledge in php is slim so I'm having difficulty trouble shooting this one. I did successfully code a simpler form with only one field. That one is sending correctly. Since this form has multiple fields, it seems to be throwing something off.
<?php
if($_POST["submit"]) {
$recipient="myemail#gmail.com";
$subject="Contact Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
$thankYou="<p>Thank you! Your message has been sent.</p>";}
?>
<?=$thankYou ?>
<form method="post" action="company.php">
<input class="contact" type="text" name="sender"
placeholder="First Name" size="25">
<input class="contact" type="text" name="last"
placeholder="Last Name" size="25">
<input class="contact" type="text" name="title"
placeholder="Title" size="25">
<input class="contact" type="text" name="business"
placeholder="Business" size="25">
<input class="contact" type="email" name="senderEmail"
placeholder="Email" size="25">
<input class="contact" type="text" name="phone"
placeholder="phone" size="25">
<textarea class="contact" name="message"
placeholder="How can we help you?" rows="4" cols="56"></textarea>
<input class="blu-btn" type="submit" name="submit"
value="Send Message">
</form>
It's not throwing any errors, I'm just not receiving the email. I've checked spam, tried a separate email, I'm missing something. Thank you so much for your help!
You should check first if your server is truly sending the mail, changing your code a bit:
if($_POST["submit"])
{
$recipient="myemail#gmail.com";
$subject="Contact Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
if (mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"))
{
echo "<p>Thank you! Your message has been sent.</p>";
}
else
{
print_r(error_get_last()["message"]);
}
}
Take a look into the PHP Documentation for mail() function
Return Values
Returns TRUE if the mail was successfully accepted for
delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Probably the server itself isn't properly configured to send email.
Is a shared hosting? Or something like?
Kind regards!
The environment where you run this makes all the difference. Mail may not be configured correctly or, some spam filter blocked it. In this case, nothing in your code can make a difference.
If you have control of the server, and you know how, you could check the mail program. If you are limited to only writing code, you have other options. You can use SMTP and send email through an external service. Then you can use mailtrap.io to capture the outbound email. This is a good way to go for debugging and making sure that your code is right.
You can use SwiftMailer if you want to try an alternative mail client.

How do I add a form on HTML and send to my email using PHP

I am attempting to code a form on my HTML website with 5 fields and get them to send to my email using PHP. However, I am not sure why I am not able to successfully do that. I am able to get a positive feedback when I echo but when I add the mailto() part that's when everything gets complicated.
Would somebody be able to explain each step I will need to take in the procedure in creating a simple form using PHP from the very beginning. I would like to be able to use this for reference when coding forms – as well as anybody else that may need a reference.
Here's a basic email program that you can use:
$to = "person#example.com";
$subject = "Hello!";
$body = "Goodbye now. I am bored. Please give me pizza.";
mail($to, $subject, $body);
You just need those three parameters, and you can easily turn them into values from your form using this:
$to = $_POST["email"];
$subject = $_POST["email-subject"];
$body = $_POST["email-body"];
When the inputs in your form are this:
<input type="email" name="email" />
<input type="text" name="email-subject" />
<input type="text" name="email-body" />
You don't even need the mailto() mentioned in your question, just use this syntax:
mail(to, subject, message, headers, parameters);
And that's how you send an email in PHP.

How can I set up a contact form on my website? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've done my research on this but can't seem to get it to work.
I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.
What am I missing? Thank you!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temp</title>
</head>
<body>
<form method="post" action="send.php" name="contact_form">
<p>
<input name="name" type="text" />
</p>
<p>
<input name="email" type="text" />
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#myemail.co";
$subject = "Contact Form Submission";
mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";
?>
I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.
Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).
Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname#mydomain.com"). Then, just include the person's name and email address in the $message of the email.
Hope that helps.

php mail function not sending email when From address is yahoo

Not sure if anyone else has experienced this but i have a simple form that sends out a email.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="phone" id="phone" value="<?php echo $phone; ?>" />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
When submitted i have the following:
if ($_POST) {
$email_to = "myemail#yahoo.com";
$subject = "Contact Form";
$message = "Phone: {$phone}\r\nMessage: {$msg}";
$headers = "From: sendingemail#yahoo.com" . "\r\n";
mail($email_to,$subject,$message, $headers);
}
When the form is submitted the mail function returns true but no email gets sent however when i change the FROM email to anything else outside of yahoo such as something#gmail.com the email comes through. Anyone know how to solve this issue?
Yahoo marked your mail as spam and is probably just ignoring it.
This is probably true for hotmail as well.
Best thing todo is find yourself a good SMTP mail pluging/module (phpMailer for instance) and use the credentials of a legit mail account. This way you are sending mail trough a dedicated mail server and changes are you won't be marked as spam anymore.
Do notice however than when you sent loads of (simular) mails or your script gets hacked and is used for spamming, changes are that your legit mailserver becomes blacklisted or (if you are lucky) blocks your account as beeing unsafe.

How do I send email via PHP from within an HTML file?

My website is HTML5. Consequently, my files are .html. I have a contact.html file that I would like to use to send a message from, using PHP. I don't have much experience with PHP (so if anyone could recommend a better alternative, non-.NET way of sending email, please let me know).
My initial thought was to include my PHP code inside my HTML file (whether or not this is possible or even recommended, I don't know). I've done this once before, and I believe I remember having a form tag that somewhere in its attributes specified the .php file that I used to send the email.
Something like <form someattribute="sendmail.php"> ... </form>.
QUESTION: Given what I THINK I should do (above), is this the best approach (specifying the PHP file inside my form tag), or do you recommend a better way to send email from a raw .html file?
You cannot do that only with HTML. If you stick to PHP solution, try
<?php
if(isset($_POST['send'])) //check the submit button was pressed
{
//get variables from POST array. Remember we specified POST method
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//set up headers
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//send the email and save the result
$result = mail($to, $subject, $message, $headers);
//was it sent?
if($result)
{
echo "Successfuly sent the email";
}
else
{
echo "An error has occured";
}
}
?>
<hr>
<form method="POST">
To: <input type="text" name="to"> <br>
Subject: <input type="text" name="subject"> <br>
Text: <textarea name="message"></textarea><br>
<input type="submit" value="Send" name="send">
</form>
You do not need to specify where the form points to because it is the same file. Otherwise it would be
<form action="somefile.php" method="POST">
Altough you have to specify the method POST, otherwise all the data will be sent through GET by default
PHP has a mail function that is used to send the email http://php.net/manual/en/function.mail.php
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
We check if the email is sent or not and print a corresponding message. Then, regardless of the result, we print out the message form.
You can easily send the mail by posting the data into a php file. Just need to write some codein that php file and in form user action='phpfilename.php'. Thats it.
If you're just trying to send the form info via e-mail it's fairly simple.
<form action="sendmail.php">
just need to make sure you're coding your php file correctly.
http://php.net/manual/en/function.mail.php
mail.html
<form action="mail.php" method="post">
To <input type="text" name="to"/><br/>
Subject <input type="text" name="subject"/><br/>
Message <textarea name="message"></textarea><br/>
<input type="submit" value="Send"/>
</form>
mail.php
<?php
mail($_POST["to"] , $_POST["subject"], $_POST["message"]);
header("Location: mail.html"); //redirect the user
?>
HTML is only client side, and is just markup so it cannot send an email. You should have a form that posts to a PHP page, as you suggest, and that PHP page sends the email.
http://www.w3schools.com/php/php_forms.asp
http://www.w3schools.com/php/php_mail.asp

Categories