Skip to main content
ikevin12
Known Participant
March 9, 2018
Answered

PHP, Yahoo Error

  • March 9, 2018
  • 2 replies
  • 6252 views

I am building a website that has a comment section for clients to type in their name, their email address, and any comments they may have for the business. This email form is sent to a php page that I have created. During my test run, the content was sent succesful to a gmail account; however, the content will not send to a yahoo email address (the business owner has a yahoo email address).

I was wondering as to why this content will send successfully to a gmail account, and not a yahoo account. The code is as follows:

<?php

$errors = '';

$myemail = 'jondoe@yahoo.com';//<-----Put Your email address here.

if(empty($_POST['customerName'])  ||

   empty($_POST['emailAddress']) ||

   empty($_POST['emailComment']))

{

    $errors .= "\n Error: all fields are required";

}

$name = $_POST['customerName'];

$email_address = $_POST['emailAddress'];

$message = $_POST['emailComment'];

if (!preg_match(

"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",

$email_address))

{

    $errors .= "\n Error: Invalid email address";

}

//above is making sure the content is valid

if( empty($errors))

{

  $to = $myemail;

  $email_subject = "Contact form submission: $name";

  $email_body = "You have received a new message. ".

  " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";

  $headers = "From: $myemail\n";

  $headers .= "Reply-To: $email_address";

  mail($to,$email_subject,$email_body,$headers);

  //redirect to the 'thank you' page

  header('Location: contact-form-thank-you.html');

}

?>

<!DOCTYPE HTML>

<html>

<head>

  <title>Contact form handler</title>

</head>

<body>

<!-- This page is displayed only if there is some error -->

<?php

echo nl2br($errors);

?>

</body>

</html>

This topic has been closed for replies.
Correct answer Nancy OShea

Ah, now I remember, Nancy.

 

I used your tutorial for a project. And it works great. But one problem I'm having now -- and maybe you can answer -- is how can I add the ability to send multiple attachments? I have been trying to figure this out from the following PHP code and form HTML. I've changed a few things in your original code and the form now sends attachments as desired, but it totally ignores the form validation and anti-bot test. I've been trying different things, but with no success. 

 

Any ideas on how to merge the relevant code below into your code?

 

Thanks,

Dave

 

PHP for multiple attachments:

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="me@mywebsite.com";
$subject="E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message="This is an example";

$message .= "Name:".$_POST["fromname"]."Message Posted:".$_POST["message"];

// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name)){

// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>

 

FORM HTML:

 

<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>Your name: <input type="text" name="fromname"></p>
<p>Your e-mail: <input type="text" name="fromemail"></p>
<p>Mod List: <textarea maxlength="1000" cols="25" rows="6" name="message"></textarea>
<p>File: <input type="file" name="file1"></p>
<p>File: <input type="file" name="file2"></p>
<p>File: <input type="file" name="file3"></p>
<p>File: <input type="file" name="file4"></p>
<p>File: <input type="file" name="file5"></p>
<p>File: <input type="file" name="file6"></p>
<p>File: <input type="file" name="file7"></p>
<p>File: <input type="file" name="file8"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>


Please DO NOT use that form from 2018.

It's too outdated for use now.

See my latest reply in your other topic.
https://community.adobe.com/t5/dreamweaver-discussions/how-do-i-merge-php-file-to-attach-and-send-multiple-attachments-with-existing-php-file/m-p/14097497#M226074

 

[This outdated discussion is locked.]

2 replies

David_Powers
Inspiring
March 11, 2018

I detect two potential problems with your script:

  • Email headers should be separated by a carriage return and newline character. You're using only a newline character. The first line of your $headers should end with "\r\n" rather than "\n".
  • The From header should indicate where the email is being sent from. You're using the same address as the one the email is being sent to. Change the From header to a genuine email address that exists on the domain that the form is hosted on.

Many mail servers reject email that comes from an unrecognized source. Using the same address as the destination is almost certainly being treated as spam.

ikevin12
ikevin12Author
Known Participant
March 11, 2018

David,

Your right about your first point, however I'm not understanding your second point. $myemail is the genuine email that is being used to receive any messages from potential clients. $email_address is the customers email address.

These two are different. Not sure what you mean by I am using the same address as the one the email is being sent to.

Nancy OShea
Community Expert
Community Expert
March 11, 2018

Create a legitimate email address on your server -- webmaster@your_domain.com.  Use that as the sending email.

Nancy O'Shea— Product User & Community Expert
Community Expert
March 9, 2018

Did you make sure it didn't end up in a spam folder at yahoo?

ikevin12
ikevin12Author
Known Participant
March 10, 2018

I did, I checked each folder over and over again. When I sent it to a gmail account it took about 5 minutes to receive.